CSV vs TSV: How to Tell the Formats Apart by Delimiter

The difference between CSV and TSV: how commas, tabs and quoting work, how to detect which format a file uses, and how to convert between them safely.

CSV and TSV both store a table as plain text. Each line is one record, and a delimiter character separates the fields. CSV uses a comma and TSV uses a tab. That choice decides how each format handles data that contains the delimiter itself, and that is where most import errors and corrupted columns start.

What CSV and TSV actually are

CSV stands for comma-separated values. RFC 4180 describes the common form. Commas separate fields and line breaks separate records. A field that contains a comma, a double quote or a line break is wrapped in double quotes, and a literal double quote inside such a field is written as two double quotes. Many programs depart from this description, so in practice "CSV" is a family of variants.

TSV stands for tab-separated values. The registered definition for the text/tab-separated-values media type separates fields with a tab character and does not allow tabs inside fields. It has no quoting mechanism. Some tools add their own escape sequences or borrow CSV-style quoting. These are tool conventions rather than a shared standard, so you can't assume another program will understand them.

The full set of rules a file follows is called its dialect. A dialect includes the delimiter, the quote character, how special characters are escaped, the line ending (LF or CRLF), whether there is a header row, and the text encoding. Detecting the delimiter is the first step, but a reliable import needs the whole dialect to match.

The same data in both formats

The illustrative example below shows one small table in each format. Tab characters are invisible, so they are written here as <TAB>. In a real file each <TAB> is a single tab character (U+0009).

name,city,note
Ana,"Portland, OR","said ""hi"""
Ben,Austin,none
name<TAB>city<TAB>note
Ana<TAB>Portland, OR<TAB>said "hi"
Ben<TAB>Austin<TAB>none

In the TSV version, the comma in "Portland, OR" and the quotes around "hi" are ordinary characters and need no special treatment. In the CSV version, the same content requires quoting, and the inner quotes must be doubled. The reverse also holds. A tab is an ordinary character inside a CSV field, but a strict TSV file cannot contain one inside a field.

Key differences at a glance

Aspect | CSV | TSV
Delimiter | Comma (,); some exports use a semicolon instead | Tab character (U+0009)
Quoting | Double quotes around fields that contain the delimiter, quotes or line breaks | None in the strict definition
Delimiter inside a field | Allowed when the field is quoted | Not allowed in strict TSV; tools that permit it rely on their own escapes
Line breaks inside a field | Allowed inside quoted fields | Not allowed in strict TSV
Splitting a line into fields | Needs a quote-aware parser | Splitting on the tab character is exact for strict TSV
Typical file extensions | .csv | .tsv, sometimes .tab or .txt
Media type | text/csv | text/tab-separated-values

How to tell which format a file uses

Don't rely on the file extension. A file named .csv can be tab- or semicolon-delimited, and a .txt file can be either format. Inspect the content instead.

  • Make tabs visible. Turn on your editor's show-whitespace option, or use a command that prints tabs as a visible marker (shown below).
  • Examine the header row. Count how many commas, tabs and semicolons it contains. The delimiter is usually the character that appears once between each pair of column names.
  • Check consistency. Look at several data rows and confirm the same candidate delimiter produces the same number of fields on each line.
  • Look for quoting. Double quotes wrapping fields that contain commas strongly suggest CSV.
  • Note the line endings and encoding. They don't change the format, but they belong to the dialect and can cause import problems if they're mismatched.

With GNU coreutils, cat -A displays each tab as ^I and marks each line end with $. The output below is illustrative and shows what the TSV example above would look like.

$ cat -A sample.txt | head -n 3
name^Icity^Inote$
Ana^IPortland, OR^Isaid "hi"$
Ben^IAustin^Inone$

If you see ^I between values, the file is tab-delimited. If a line ends in ^M$ rather than $, it uses Windows-style CRLF line endings.

To check consistency, count fields per line for a candidate delimiter. The first command below tests tab and the second tests comma.

awk -F'\t' '{print NF}' sample.txt | sort | uniq -c
awk -F',' '{print NF}' sample.txt | sort | uniq -c

If every line reports the same field count, that delimiter is a good candidate. These commands do not understand quoting. A valid CSV file with quoted commas, such as "Portland, OR", will show uneven counts even though nothing is wrong. For CSV, confirm the result with a quote-aware parser.

Detecting the dialect with a parser

Python's standard library includes csv.Sniffer, which guesses the dialect from a sample of the file. It is a heuristic. It can guess wrong, and it raises csv.Error when it cannot decide. Both are more likely with short samples or single-column data. Limiting the candidate delimiters to the ones you expect improves the guess.

import csv

with open("sample.txt", newline="", encoding="utf-8") as f:
    sample = f.read(4096)

dialect = csv.Sniffer().sniff(sample, delimiters=",;\t")
print(repr(dialect.delimiter))

A printed value of '\t' means tab and ',' means comma. Treat this as a guess, not a final answer. Parse the whole file with the detected dialect and check that every row has the expected number of fields before you rely on it.

Which format fits your data

Neither format is better in general. The right choice depends on what your fields contain and what the receiving tool expects.

  • Fields contain commas, such as addresses, prose or numbers with thousands separators: TSV avoids quoting entirely, as long as no field contains a tab or a line break.
  • Fields contain tabs or line breaks, such as free text or pasted content: CSV with proper quoting can represent them, and strict TSV cannot.
  • The receiving system has documented requirements: follow its documented import rules rather than a general preference.
  • You are exchanging data with spreadsheet or database tools: these generally offer CSV import and export. Some regional settings produce semicolon-delimited files, typically where the comma is the decimal separator, so confirm the delimiter instead of assuming a comma.
  • You are processing data line by line with tools like cut or awk: strict TSV works well, because each line is one record and splitting on tab is exact. CSV needs a quote-aware parser.
  • Repeatability matters: whichever format you choose, write down the full dialect (delimiter, quote character, encoding, header row and line ending) so that later exports and imports match.

Converting between CSV and TSV without damaging content

Don't convert by replacing every comma with a tab. That also replaces commas inside quoted fields, which splits values like "Portland, OR" into two columns, and it leaves the CSV quote marks behind as stray characters. Use a parser that reads one format and writes the other. The example below is illustrative.

import csv

with open("input.csv", newline="", encoding="utf-8") as src, \
     open("output.tsv", "w", newline="", encoding="utf-8") as dst:
    reader = csv.reader(src)
    writer = csv.writer(dst, delimiter="\t", lineterminator="\n")
    for row in reader:
        writer.writerow(row)

With default settings, Python's csv.writer still wraps a field in quotes if it contains the delimiter, the quote character or a line break. A field like said "hi" would therefore be written in quotes, with the inner quotes doubled. The output is tab-delimited but uses CSV-style quoting, which a strict TSV reader may not interpret correctly. If the target expects strict TSV, first scan the data for tabs, line breaks and quote characters. Decide how to handle any you find, then set the writer's quoting options to match what the receiving tool documents.

To verify the conversion, read the output back with the intended dialect. Confirm that the row count matches the source and that the field values match exactly, including whitespace, quotes and non-ASCII characters.

Limits of this guide

No measurements or product-specific test results were available for this guide, so it makes no performance claims and does not describe how any particular spreadsheet or import tool behaves. Delimiter defaults, quoting support and encoding handling vary between applications, versions and regional settings. Check the documentation of the tools you actually use.