CSVCSV GUIDE
Home›CSV Complete Guide›Reading and writing in code
Detail Guide 04 · Implementation

Reading and writing CSV files in code

"python read csv" and "c# read csv" are the core developer searches around CSV. This page collects the standard code for each language and the encoding settings that Japanese CSV files demand, with links to the official documentation throughout.

Updated October 30, 2026Reading time about 12 minutesSources official documentation only
THE SHORT ANSWER

The baseline is to state the encoding explicitly. In Python, open(path, encoding="cp932"); in pandas, read_csv(encoding=...); in VBA, TextFilePlatform (65001 for UTF-8, 932 for Shift-JIS); in C#, Encoding.GetEncoding(932). Because commas can appear inside quoted fields, use your language's CSV library rather than splitting on commas.

TOOLTools that help while you buildEverything described on this page can be done in the browser.
Contents
  1. Language cheat sheet
  2. Python (standard csv)
  3. Python (pandas)
  4. Excel VBA
  5. C#
  6. Writing CSV
  7. Encoding cheat sheet
  8. Frequently asked questions
01Choose

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.

LanguageStandard libraryHow to set the encodingPrimary source
Pythoncsv standard module / pandasencoding= / open(..., encoding=)csv module docs / read_csv docs
Excel VBAQueryTables / OpenTextTextFilePlatform (65001 / 932)Microsoft Learn
C#TextFieldParser / CsvHelperEncoding.GetEncoding(932) and so onMicrosoft Learn
02Code

Python (standard csv module)

Python
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.

03Code

Python (pandas)

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:

OptionWhat it doesWhen to use it
encodingSets the character encodingcp932 / utf-8 / utf-8-sig
dtypePins a column's typeProtects leading zeros in codes
sepSets the field delimiterTSV needs sep="\t"
headerChooses the header rowHeaderless CSV needs header=None
skiprowsSkips leading rowsExcel exports with a title row on top
chunksizeReads in chunksVery large files (millions of rows)
04Code

Excel VBA

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.

05Code

C#

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.

06Code

Writing CSV (exporting)

  • pandas — df.to_csv(path, index=False, encoding="utf-8-sig"). Forget index=False and you get an extra row-number column (docs).
  • Python standard library — csv.writer with writerow(). Convert numbers to strings before passing them in (docs).
  • VBA — SaveAs FileFormat:=xlCSVUTF8 writes 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.

07Reference

Encoding cheat sheet

Where the file came fromValue to pass
Saved from Excel as "CSV UTF-8"utf-8-sig
Exported by a web or cloud serviceutf-8
Japanese business software (accounting packages, ERP exports)cp932
Old government datasetsshift_jis (use cp932 if some fields break)
UnknownDetect it with chardet or charset-normalizer first

The detailed repair procedure is on the dedicated encoding page.

Sources (checked October 30, 2026)
08FAQ

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.