๐งฐ File Operations ยท 6 min read
CSV Delimiter in Python
The built-in csv module splits fields on a comma unless you say otherwise: pass delimiter=";" to csv.reader or csv.DictReader, let csv.Sniffer() guess the separator, or pick a Dialect like excel-tab for tab files. If you leave the default on a file that uses something else, the module does not complain โ it hands you one column whose single value contains the whole line. That silent wrong shape is why the question keeps coming up.
The default is a comma, and a semicolon file becomes one column
The csv module documents delimiter as a one-character string with a default of ','. There is no detection unless you ask for it. So a file exported by software that writes semicolons โ the common case in locales where the comma is the decimal mark โ is not rejected. It is read as a single field per line.
Measured on Python 3.13.5 with a three-row file whose header is id;name;city:
| Call | Fields per row | First value |
|---|---|---|
csv.reader(f) | 1, 1, 1, 1 | id;name;city |
csv.reader(f, delimiter=";") | 3, 3, 3, 3 | id, name, city |
csv.reader(f, dialect=sniffed) | 3, 3, 3, 3 | sniffed delimiter ';' |
csv.reader(f, dialect="excel-tab") | 3, 3 | tab file split correctly |
The first row is the failure mode worth memorising: one field that contains the entire header line. The same symptom shows up in Excel, which is covered in why a CSV opens in one column.
delimiter and Dialect cover two different needs
delimiter changes one setting. A Dialectchanges a whole bundle of them โ the delimiter, the quote character, the quoting rule, and the line terminator โ and the module ships two ready-made ones. excel is comma-delimited and matches how Excel writes CSV. excel-tab is tab-delimited and matches a file pasted out of a spreadsheet. You can register your own with csv.register_dialect("pipe", delimiter="|") if a particular vendor format shows up often.
The tab case is worth its own note because it looks the same as the semicolon case from the outside. A tab-separated file read with the comma default lands in one column, and the fix is delimiter="\t" or dialect="excel-tab". The general question of which delimiter a file actually uses is covered in CSV delimiter options.
Let Python guess with csv.Sniffer
csv.Sniffer().sniff(sample, delimiters=",\t;|") returns aDialect whose delimiter attribute holds the guess. The second argument is a list of candidate characters, so an unusual separator is detectable as long as it is on the list. On the semicolon file above, the sniffer returned ';' and the rows came back with three fields.
Two details matter in practice. It is a heuristic, so a very short file or a file with a preamble of comment lines can be guessed wrong. And it only helps you read, not write. For a one-off look at an export you did not create, sniffing is the right call; for a script that reads the same file shape every day, hard-code the delimiter.
Writing with a non-comma delimiter
The same argument goes on csv.writer: csv.writer(f, delimiter=";") emits semicolon-separated output. Keep the default quoting mode, QUOTE_MINIMAL, so a field that contains the delimiter is wrapped in double quotes and survives the round trip. A value like Smith, John inside a semicolon file stays one field because the writer quotes it. The rules that decide when quoting happens are explained in quotes in CSV files.
One Windows-specific trap: if your output comes out with a blank line between every record, the cause is the file being opened without newline="", not the delimiter. The writer already ends each record with \r\n, and the text layer translating newlines a second time is what doubles them. The full explanation is in why csv.writer adds blank rows.
Python csv module vs pandas
pandas uses a different argument name: sep, with delimiter as an alias, and it supports multi-character and regex separators that the csv module does not. The csv module takes a single character and has no regex path. If you are already using pandas, the pandas separator guide covers sep, sep=None and the C-engine regex error. If you just need to read a file once, the stdlib csv module is enough and needs no install.
Fixing the file instead of the code
Setting the delimiter in code is the right fix when your script is the only consumer. It is the wrong fix when anything else will open the file โ a spreadsheet, an upload form, a colleague. In that case the file should be a real comma CSV, and changing the delimiter explains what has to stay intact when you rewrite it: quoting, embedded line breaks, and the mistake of running find-and-replace on a file whose values contain commas. You can also drop the file into the delimiter converter once and read a clean file afterwards.
Frequently Asked Questions
How do I set the CSV delimiter in Python?
Pass the delimiter argument to csv.reader or csv.DictReader: csv.reader(f, delimiter=';') for semicolons, delimiter='\t' for tabs, delimiter='|' for pipes. The default is ','. The argument takes a one-character string, so a multi-character separator is not supported by the csv module the way it is in pandas.
Why does csv.reader put my whole file in one column?
Because the file uses a separator other than the comma and you did not tell csv.reader about it. We measured it on Python 3.13.5 with a three-row file whose header is id;name;city: the default call returned four single-element rows, and the first row was the string 'id;name;city'. Passing delimiter=';' returned four rows of three fields each.
How does csv.Sniffer detect the delimiter?
csv.Sniffer().sniff(sample, delimiters=',;\t|') inspects the sample text and returns a Dialect object whose delimiter attribute holds the guess. The delimiters argument lists the candidate characters, so a file using an unusual separator can still be detected as long as the character is in the list. It is a heuristic, so a very short or irregular file can be guessed wrong.
What are Python CSV Dialects like excel and excel-tab?
A Dialect is a bundle of parsing settings: the delimiter, quote character, quoting rule, and line terminator. The csv module ships two built-in dialects: 'excel' (comma-delimited, as Excel writes CSV) and 'excel-tab' (tab-delimited). You can pass one by name with dialect='excel-tab', or register your own with csv.register_dialect.
Should I detect the delimiter or just hard-code it?
Hard-code it when you control the source and the file shape never changes. Use csv.Sniffer for one-off reads of exports you did not create, where the separator varies. Detection runs a heuristic, so for a nightly job reading the same format every time, a fixed delimiter is more predictable.
How do I write a CSV with a different delimiter in Python?
Pass delimiter=';' (or any single character) to csv.writer. Keep the default quoting, QUOTE_MINIMAL, so a field that contains the delimiter gets wrapped in double quotes and survives a round trip. The extra blank-line problem that shows up on Windows comes from opening the output without newline=''; open the file with newline='' and the module writes the correct line endings.
Sources
- Python standard library documentation, csv โ CSV File Reading and Writing:
delimiteris a one-character string with a default of',';csv.readerandcsv.DictReadertake it;csv.Snifferguesses aDialectfrom a sample; the module registers two dialects,excelandexcel-tab; and the defaultfield_size_limitis 131,072. Retrieved 2026-11-16. - RFC 4180, sections 2 and 3: records are separated by CRLF, fields by a comma, and fields containing commas, quotes or line breaks are enclosed in double quotes. The registered media type is
text/csv. Retrieved 2026-11-16. - Local measurement, 2026-11-16, Python 3.13.5: a three-row semicolon file with header
id;name;cityread with the default returned four single-element rows and a first value ofid;name;city; withdelimiter=";"it returned four rows of three fields;csv.Sniffer().sniff(sample, delimiters=",\t;|")returned delimiter';'and the same three fields; a tab file read withdialect="excel-tab"returned rows of three fields.
Stop Guessing the Separator
Drop the file into the delimiter converter and it detects the actual separator, then rewrites the file as a standard comma CSV with quoting intact โ so the Python call works with no special argument.
Related reading
File Operations โ other guides that pair well with this one.
- Count Rows in a CSV File
- Import CSV into Google Sheets
- How to Open a CSV in Excel With Columns (Not One Column)
- Can ChatGPT Create a CSV File? Yes, and Here's Where It Goes Wrong
Browse all guides in the NoCodeCSV blog.