Read and write tab-delimited CSV files

How tab-delimited CSV (TSV) files work, what changes when you switch from commas to tabs, how to read and write them in code, and when the format holds up after copying and sharing.

A "CSV with tab delimiter" is a delimited text file that separates fields with the tab character instead of a comma. It is often called TSV (tab-separated values). This review covers what switching the delimiter changes, how to read and write these files reliably, and when the format stays usable after copying or sharing. No hands-on tests were run for this article. Behavior described here comes from general format conventions and documented library behavior, and any tool-specific result should be checked in your own environment.

Is a tab-delimited file the right choice?

Tabs are a good choice when your data often contains commas, such as addresses, prose notes, number formats that use commas, or names written as "Last, First". Tab characters rarely appear inside ordinary field values, so most rows need no quoting, and the raw file is easier to scan and less likely to be split incorrectly by a simple parser.

The main tradeoff is that the delimiter is invisible. A tab looks like spaces on screen, so a person editing the file by hand can't easily tell whether a gap is one tab, several spaces, or an empty field. Many editors, chat tools and web forms convert or strip tabs, and the file can break silently.

Comma-separated CSV is usually the safer default when the receiving system expects standard CSV, when people will edit the file by hand, or when the data passes through tools that may rewrite whitespace. Plain prose or a formatted table is better than either when the goal is human reading rather than machine import.

What changes when you use a tab delimiter

Switching the delimiter changes the stored characters in the file. Each separating comma is replaced with the tab character (Unicode U+0009). This is a real change to the data, not a display setting. A program reading the file has to be told which separator to expect, or it will treat each line as a single field.

The terms can be confusing. "CSV" literally means comma-separated values, and the widely cited CSV specification (RFC 4180) describes commas. In practice, many tools and people still say "CSV" for any delimited text file. A file named .csv may contain tabs, and a file named .tsv may be read by a CSV parser that has been configured for tabs. Always check the actual separator rather than trusting the file extension.

Quoting rules are the second change. In comma-delimited CSV, a field that contains a comma must be wrapped in double quotes. With a tab delimiter, commas are ordinary characters and need no quotes. Tabs, line breaks and quote characters inside a field still need handling. The common TSV convention registered as a media type has no quoting mechanism, so under that convention fields simply may not contain tabs. CSV libraries configured for tabs usually keep CSV-style quoting instead. If the producer and consumer follow different conventions, values with quotes, tabs or line breaks can be misread.

The example below shows the same two data rows as comma-delimited input and tab-delimited output. In the second row, the quotes around "Lee, Sam" were only needed because of the comma delimiter, so a CSV writer using minimal quoting would leave them out in the tab version. The gaps in the tab version are single tab characters.

Comma-delimited input:
id,name,note
1,Ada,ok
2,"Lee, Sam",needs review

Tab-delimited output:
id	name	note
1	Ada	ok
2	Lee, Sam	needs review
Aspect | Comma delimiter | Tab delimiter
Separator character | , (U+002C) | tab (U+0009)
Commas inside values | Must be quoted | No quoting needed
Tabs inside values | Usually no quoting needed | Must be quoted, escaped or removed, depending on convention
Visible in a text editor | Yes | Looks like whitespace
Risk from whitespace-altering tools | Low | Higher: tabs may become spaces
Common name | CSV | TSV, or tab-delimited CSV

Reading a tab-delimited file in code

Most CSV readers accept a delimiter or separator option. In Python's standard csv module, pass the tab escape sequence as the delimiter. Open the file with newline set to an empty string so the module can handle line endings and quoted line breaks itself.

import csv

with open("input.tsv", newline="", encoding="utf-8") as f:
    reader = csv.reader(f, delimiter="\t")
    for row in reader:
        print(row)

The delimiter has to be the actual tab character, not the two characters backslash and t. This usually goes wrong when the delimiter comes from a command-line argument or a config file, where \t may arrive as literal text. Python's csv module rejects a delimiter that is not a single character, so convert the value before passing it in.

Some tab-delimited files come from systems that never quote fields, and their data may contain stray double quotes. A default CSV reader would treat those quotes as special. For such files, Python's csv module lets you turn quote processing off with quoting=csv.QUOTE_NONE, so that every character between tabs is read as-is.

In pandas, the equivalent option is the sep parameter:

import pandas as pd

df = pd.read_csv("input.tsv", sep="\t")

Writing a tab-delimited file in code

Writing uses the same delimiter option. With Python's default minimal quoting, the writer quotes only fields that contain the delimiter, the quote character or a line break. In a tab-delimited file, that means fields containing commas are written without quotes.

import csv

with open("output.tsv", "w", newline="", encoding="utf-8") as f:
    writer = csv.writer(f, delimiter="\t")
    writer.writerow(["id", "name", "note"])
    writer.writerow([1, "Ada", "ok"])
    writer.writerow([2, "Lee, Sam", "needs review"])
df.to_csv("output.tsv", sep="\t", index=False)

If the receiving system follows the strict no-quoting TSV convention, quoted fields will reach it with their quote marks attached. In that case, clean the data before writing: replace tabs and line breaks inside values, for example with a space, so that no quoting is ever needed. Confirm what the receiving system expects rather than assuming.

Does it stay readable and usable after copying or sharing?

Copying and pasting is where tab-delimited data is most fragile. These risks apply generally, and how much each one affects you depends on the tools involved:

  • Editors set to insert spaces in place of tabs, or to reformat indentation, can turn delimiters into spaces when a file is edited and saved.
  • Chat apps, email clients, web forms and document editors may collapse, convert or drop tab characters in pasted text.
  • Tools that trim trailing whitespace can remove tabs at the end of a line, which deletes empty trailing fields and shortens rows.
  • Pasting tab-separated text into a spreadsheet often splits it into columns, but this depends on the application and its import settings. Check it in the specific spreadsheet you use.

Editing raw files by hand is more error-prone with tabs because the separators are invisible. Turning on a "show whitespace" or "show invisibles" option in your editor helps. A quick check that every row has the same number of fields will catch most damage after a transfer.

Searching inside the data works normally for the field values themselves. Searching for the delimiter is harder, because many search boxes do not accept a typed tab character. In those tools you may need an escape sequence or a regular expression.

No documentation or tests were available here on how specific screen readers announce raw tab-delimited text, so no claim is made about any particular assistive technology. In general, a raw delimited file is not a reading format. When people, including screen reader users, need to read the data, give them a properly structured table, such as a spreadsheet with header cells or a document table, instead of the raw text.

When you need a plain or alternative version

  • Send comma-delimited CSV, or the format the recipient documents, when the destination expects standard CSV.
  • Share the file as an attachment rather than pasting its contents into a message or form, so the tabs are not altered.
  • Provide a formatted table or a short plain-text summary when the audience is people rather than software.
  • Include a note stating the delimiter and quoting convention whenever the file extension or file name could be ambiguous.

Bottom line

Tab-delimited CSV is a good fit for machine-to-machine exchange of data full of commas, because it mostly removes the need for quoting. Its weakness is the invisible delimiter, which tools that rewrite whitespace can damage and which is harder to edit by hand. Use it when you control both ends of the exchange or the recipient documents tab support, set the delimiter explicitly when reading and writing, and agree on a quoting convention. Use comma-delimited CSV or a formatted table when the data will pass through unknown tools or be read mainly by people.