Convert tab-delimited text to CSV

How converting a tab-separated file to CSV works, what changes besides the delimiter, and how to keep commas, quotes, leading zeros and encoding intact.

Tab-delimited text (often saved as .tsv or .txt) and comma-separated values (CSV) both store a table as plain text, one record per line. Converting between them looks like a simple swap of tabs for commas. Usually it is, but only if the conversion also applies CSV's quoting rules and the program that opens the result reads the values as intended. This review covers when the conversion is worth doing, what it changes in the stored characters, and where data can be altered without warning.

Is converting TSV to CSV the right move?

Convert when the destination expects CSV, such as an import form, a database loader or a tool that only lists comma-separated files as accepted input. CSV is the more widely recognized of the two formats, and that broad acceptance is the main benefit of converting.

The key tradeoff is quoting. A tab rarely appears inside ordinary text, so tab-delimited data often needs no quoting. Commas are common in names, addresses, prices and free-text notes. Every field that contains one must be wrapped in double quotes in CSV. A correct converter handles this automatically. A plain find-and-replace does not, and it quietly shifts values into the wrong columns.

Keep the tab-delimited file if the destination accepts it, if the data has many commas and you have to inspect or edit the file by hand, or if a downstream script already parses tabs. A file that works where it needs to work gains nothing from conversion.

What the conversion actually changes

This is a change to the stored characters, not to how the text is displayed. The field separator becomes a comma. Some fields gain surrounding double quotes. Double quotes inside a quoted field are doubled. Line endings may also change, because the common CSV specification (RFC 4180) uses CRLF line breaks, and many converters follow it. The cell values themselves should not change.

Under RFC 4180, a field must be quoted if it contains a comma, a double quote or a line break. A double quote inside such a field is written as two double quotes. The tab-separated format registered with IANA works more simply: fields cannot contain tabs or line breaks, and it defines no quoting mechanism.

Example input (fields separated by tab characters):

id	name	note
007	Smith, Jane	Said "hello"

Correct CSV output:

id,name,note
007,"Smith, Jane","Said ""hello"""

The name field is quoted because it contains a comma. The note field is quoted because it contains double quotes, and each inner quote is doubled. A naive replacement would produce 007,Smith, Jane,Said "hello". A CSV reader would split that line into four fields instead of three.

Real-world tab-delimited files do not all follow the strict format. Some programs write embedded tabs and newlines as backslash escapes such as \t and \n. Others wrap fields in double quotes the way CSV does. Before converting, check how your source represents these cases. A converter that assumes the wrong convention will either leave escape sequences as literal text or treat quote characters as structure.

A minimal local conversion

Python's standard csv module handles CSV quoting on output. The reader below treats quote characters in the tab-delimited input as ordinary text, which fits strict TSV. If your source quotes its fields, remove the quoting argument. If it uses backslash escapes, decode them separately.

import csv

with open("input.tsv", newline="", encoding="utf-8") as src, \
     open("output.csv", "w", newline="", encoding="utf-8") as dst:
    reader = csv.reader(src, delimiter="\t", quoting=csv.QUOTE_NONE)
    writer = csv.writer(dst)
    writer.writerows(reader)

Spreadsheet applications and dedicated converters can do the same job. No specific tool was tested for this article, so check any tool's output against a sample that contains commas, quotes and line breaks before relying on it.

Will the CSV stay readable and usable after sharing?

The CSV file is still plain text, so copying and pasting it, searching it and editing it in a text editor all work. Row order and column order do not change. Most problems appear when another program opens the file and interprets it:

  • Type guessing: spreadsheet programs commonly convert values on open. They may strip leading zeros from codes like 007, reformat long numbers or turn text that looks like a date into a date. Importing with the columns set to text, rather than double-clicking the file, avoids this in many applications.
  • Encoding: save as UTF-8 when the data contains accented letters or non-Latin scripts. Some programs only detect UTF-8 when the file starts with a byte order mark, while others treat that mark as stray characters in the first header. Check which behavior your destination expects.
  • Regional separators: in some locales, spreadsheet software uses a semicolon as the list separator. A comma-delimited file may then open as a single column unless you choose the delimiter during import.
  • Line breaks inside fields: quoted multi-line values are valid CSV, but some simple importers and line-by-line scripts split them into separate records.

Accessibility depends on where the file ends up. Opened as a grid in a spreadsheet, the data is presented as cells and column headers, much as the tab-delimited version would be. Read as raw text, CSV with heavy quoting is harder to follow by eye and with a screen reader than the same data separated by tabs, because the quote marks and doubled quotes are read as content. No assistive technology was tested for this article. If people need to read the data directly, a properly marked-up table or the original tab-delimited text is often easier to use than raw CSV.

Privacy when using online converters

Whether an online converter keeps uploaded data cannot be judged from its design or its claims about speed and security. If the file contains personal, financial or confidential data, read the service's published data-handling terms, or convert locally with a spreadsheet program or a short script so the file never leaves your machine.

Checks before you rely on the result

  • Each row has the same number of fields as the header.
  • Values containing commas or quotes match the original after the CSV is read back in.
  • Identifiers keep their leading zeros, and long numbers are not shortened or written in scientific notation.
  • Non-ASCII characters display correctly in the destination.
  • The destination accepts the line endings and the presence or absence of a byte order mark.

Gaps in this review

No product-specific testing, performance figures or tool comparisons were available. The guidance above rests on the published CSV and tab-separated format conventions and on general parsing behavior. How a particular spreadsheet, database or online converter behaves should be confirmed in its own documentation or with a small test file.