Language cheat sheet
When you handle Japanese CSV files in code, the encoding is the biggest hurdle. In every language the rule is the same: state the encoding explicitly.
| Language | Standard library | How to set the encoding | Primary source |
|---|---|---|---|
| Python | csv standard module / pandas | encoding= / open(..., encoding=) | csv module docs / read_csv docs |
| Excel VBA | QueryTables / OpenText | TextFilePlatform (65001 / 932) | Microsoft Learn |
| C# | TextFieldParser / CsvHelper | Encoding.GetEncoding(932) and so on | Microsoft Learn |
Python (standard csv module)
import csv
# Read a Shift-JIS CSV. Forget newline="" and you get doubled line breaks
with open("sales.csv", encoding="cp932", newline="") as f:
reader = csv.DictReader(f)
for row in reader:
print(row["product_name"], row["sales"])
The official docs for DictReader explain that it turns the first row into dictionary keys, so your code keeps working even if the column order changes. Splitting on split(',') yourself will shift columns whenever a comma appears inside quotes, which is why you should always use the csv module.
Python (pandas)
import pandas as pd
df = pd.read_csv("sales.csv", encoding="cp932", dtype={"product_code": str})
df.to_csv("out.csv", index=False, encoding="utf-8-sig")
The main read_csv options, limited to the ones you actually use at work:
| Option | What it does | When to use it |
|---|---|---|
encoding | Sets the character encoding | cp932 / utf-8 / utf-8-sig |
dtype | Pins a column's type | Protects leading zeros in codes |
sep | Sets the field delimiter | TSV needs sep="\t" |
header | Chooses the header row | Headerless CSV needs header=None |
skiprows | Skips leading rows | Excel exports with a title row on top |
chunksize | Reads in chunks | Very large files (millions of rows) |
Excel VBA
Sub ReadCsv()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets(1)
With ws.QueryTables.Add(Connection:="TEXT;C:\data\sales.csv", Destination:=ws.Range("A1"))
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
.TextFilePlatform = 65001 ' UTF-8. Use 932 for Shift-JIS
.TextFileColumnDataTypes = Array(2, 1, 1) ' Column 1 as text
.Refresh
End With
End Sub
QueryTables.Add (Microsoft Learn) lets you control both the encoding and the column types, which makes it more reliable than Workbooks.OpenText. The TextFilePlatform values are 65001 for UTF-8 and 932 for Shift-JIS.
C#
// On .NET Core / .NET 5+ register the CodePages provider first
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
using var parser = new TextFieldParser(@"C:\data\sales.csv", Encoding.GetEncoding(932));
parser.SetDelimiters(",");
parser.HasFieldsEnclosedInQuotes = true;
while (!parser.EndOfData)
{
string[] fields = parser.ReadFields()!;
Console.WriteLine(string.Join(" / ", fields));
}
TextFieldParser (Microsoft.VisualBasic.FileIO) handles commas inside quoted fields correctly. To use Encoding.GetEncoding(932) on .NET Core or later you need a reference to the System.Text.Encoding.CodePages package and the provider registration above. In practice many teams use CsvHelper (official site) instead.
Writing CSV (exporting)
- pandas —
df.to_csv(path, index=False, encoding="utf-8-sig"). Forgetindex=Falseand you get an extra row-number column (docs). - Python standard library —
csv.writerwithwriterow(). Convert numbers to strings before passing them in (docs). - VBA —
SaveAs FileFormat:=xlCSVUTF8writes UTF-8. - C# —
WriteRecords()from CsvHelper is the usual choice.
After exporting, always open the file once in a text editor and check the column count, the quoting and the encoding before you hand it over.
Encoding cheat sheet
| Where the file came from | Value to pass |
|---|---|
| Saved from Excel as "CSV UTF-8" | utf-8-sig |
| Exported by a web or cloud service | utf-8 |
| Japanese business software (accounting packages, ERP exports) | cp932 |
| Old government datasets | shift_jis (use cp932 if some fields break) |
| Unknown | Detect it with chardet or charset-normalizer first |
The detailed repair procedure is on the dedicated encoding page.
Frequently asked questions
Q.Reading a CSV in Python throws UnicodeDecodeError
The encoding you passed does not match the file. Try utf-8 first, then cp932; if you still cannot tell, detect it with chardet. For the full procedure see the diagnosis steps on the encoding page.
Q.read_csv strips the leading zeros
Read the column as a string, for example dtype={"code": str}. Once the data has been read you cannot get the zeros back.
Q.What is the difference between csv.reader and csv.DictReader?
csv.reader gives you columns by index (0, 1, 2 and so on); DictReader gives you columns by header name. If the column order might change, DictReader is the safer choice.
Q.How do I read a huge CSV (millions of rows)?
With pandas, read it in chunks via chunksize; alternatively let DuckDB read the file directly and aggregate it with SQL. Excel has a row limit, so it is out of the question here.