How to Specify the CSV Delimiter in pandas read_csv
Set the delimiter or separator in pandas read_csv with sep or delimiter, handle tabs, semicolons, pipes and whitespace, auto-detect the dialect and check the result.
Not every "CSV" file uses commas. Exports from spreadsheets, databases and regional software often separate fields with semicolons, tabs, pipes or runs of spaces. If pandas reads such a file with the default comma setting, you usually get a DataFrame with a single column that holds each entire line. This guide shows how to tell read_csv which separator to use, how to let pandas detect it, and how to confirm the data was split correctly without altering the values.
What the delimiter setting does
A delimiter, also called a separator, is the character or pattern that marks where one field ends and the next begins on each line of a text file. pandas.read_csv takes delimited text from a file path, URL-like source or file-like object and returns a DataFrame. The separator setting controls only how each line is split into columns. It does not change the text inside fields. Quoting, decimal marks, encoding and header handling are separate parameters.
read_csv accepts two parameter names for this setting: sep and delimiter. The documentation describes delimiter as an alias for sep. Use one or the other, not both. Passing both raises an error. The default for sep is a comma.
Basic syntax
import pandas as pd
# Semicolon-separated file
df = pd.read_csv("data.csv", sep=";")
# Same thing using the alias
df = pd.read_csv("data.csv", delimiter=";")
Common separators and the value to pass
| Separator in the file | Value to pass | Notes |
| Comma , | sep="," (default) | No argument needed |
| Semicolon ; | sep=";" | Common in exports that use a comma as the decimal mark |
| Tab (invisible character) | sep="\t" | Typical for .tsv files; write the escape sequence, not a literal tab |
| Pipe | | sep="|" | A single character is treated literally |
| One or more spaces or tabs | sep=r"\s+" | Treated as a regular expression |
| Unknown | sep=None, engine="python" | pandas tries to detect the separator |
Step by step: read a file with a non-comma separator
- 1. Look at the raw text before you parse it. Open the first few lines in a plain-text editor, or print them in Python, so you can see which character separates the fields.
- 2. Pass that character to sep in read_csv.
- 3. Check the column count and the first rows to confirm the split worked.
- 4. If numbers or dates look wrong, adjust related parameters such as decimal or thousands. Do not change the separator for this.
Here is a way to inspect the raw lines. Using repr makes invisible characters visible. A tab shows up as \t and a trailing newline as \n.
with open("data.csv", encoding="utf-8") as f:
for _ in range(3):
print(repr(f.readline()))
Illustrative example. Suppose the inspection prints lines like the following. This sample is for explanation only and its output has not been run here.
'id;name;price\n'
'1;Widget;3,50\n'
'2;Gadget;12,00\n'
The fields are separated by semicolons, and the prices use a comma as the decimal mark. A matching call would be:
df = pd.read_csv("data.csv", sep=";", decimal=",")
Without decimal=",", pandas would keep values such as 3,50 as text rather than numbers. The separator is still correct in that case. The decimal mark is a separate setting.
Tab-separated files
A tab is invisible in most editors, so it is easy to mistake for spaces. Pass the escape sequence "\t". Do not paste a literal tab into your code, because it is hard to see and easy to replace with spaces by accident.
df = pd.read_csv("data.tsv", sep="\t")
Whitespace-aligned files
Some text files align columns with a varying number of spaces. A single-space separator would create empty columns wherever several spaces appear in a row. Use the regular expression \s+ instead. It matches one or more whitespace characters, including spaces and tabs.
df = pd.read_csv("report.txt", sep=r"\s+")
Older code may use delim_whitespace=True for the same purpose. That parameter was deprecated in pandas 2.2, and sep=r"\s+" is the recommended replacement. Be aware that this pattern also splits on spaces inside values, so a field such as New York would become two columns. If values contain spaces, the file needs a different delimiter or quoting.
Multi-character and regex separators
pandas interprets a separator longer than one character, other than "\s+", as a regular expression. This has three consequences:
- Regex parsing requires the Python parsing engine. pandas switches to it automatically, which can be slower than the default C engine, and it may emit a ParserWarning unless you pass engine="python" yourself.
- Characters with special meaning in regular expressions must be escaped. A file that uses || as its separator needs sep=r"||". Unescaped, || means "empty string or empty string".
- The pandas documentation warns that regex delimiters are prone to ignoring quoted data. A separator that appears inside a quoted field may still split that field.
# Fields separated by a double pipe, e.g. a||b||c
df = pd.read_csv("data.txt", sep=r"\|\|", engine="python")
Letting pandas detect the separator
If you do not know the separator in advance, pass sep=None together with engine="python". pandas then uses Python's built-in csv.Sniffer to guess the dialect from the start of the file. A dialect is the set of formatting conventions a file follows, such as its delimiter and quote character.
df = pd.read_csv("unknown.csv", sep=None, engine="python")
Detection is a heuristic, not a guarantee. It can guess wrong on files with very few columns, inconsistent rows, or a candidate character that also appears often inside values. To see what the sniffer chooses before you load the whole file, run it on a sample yourself:
import csv
with open("unknown.csv", encoding="utf-8", newline="") as f:
sample = f.read(4096)
dialect = csv.Sniffer().sniff(sample, delimiters=",;\t|")
print(repr(dialect.delimiter))
Restricting the delimiters argument to the characters you consider plausible makes a wrong guess less likely. If the sniffer cannot decide, it raises csv.Error. In that case, inspect the raw lines and set sep explicitly.
Separators inside quoted values
With a single-character separator and the default parser, a delimiter inside a quoted field is kept as part of the value. In the illustrative line below, the comma inside the quotes stays in the name column instead of creating a new column:
id,name,city
1,"Smith, Jane",Boston
If a file uses a quote character other than the double quote, set quotechar so the separator inside those quotes is protected the same way.
Verifying the result
After loading, check that the structure matches what you saw in the raw text:
print(df.shape) # (rows, columns)
print(df.columns.tolist())
print(df.head())
print(df.dtypes)
- One column whose name contains separator characters, such as 'id;name;price', means the separator is wrong.
- More columns than expected, many of them empty, often means a single space was used where \s+ was needed, or the separator also appears inside unquoted values.
- Numeric columns showing as object type may point to a decimal or thousands setting issue rather than a separator problem.
- A "Expected N fields, saw M" error usually means some rows contain extra separators, often inside values that are not quoted.
Quick reference
pd.read_csv(path) # comma (default)
pd.read_csv(path, sep=";") # semicolon
pd.read_csv(path, sep="\t") # tab
pd.read_csv(path, sep="|") # pipe
pd.read_csv(path, sep=r"\s+") # runs of whitespace
pd.read_csv(path, sep=r"\|\|", engine="python") # multi-character, escaped regex
pd.read_csv(path, sep=None, engine="python") # auto-detect
Scope note: this guide covers documented read_csv parameters and general parsing behavior. It does not include performance measurements or version-by-version compatibility details beyond the delim_whitespace deprecation noted above. Check the documentation for the pandas version you have installed if exact behavior matters.