Delimited data conversion
Convert TSV to CSV and Preserve Embedded Commas
Convert TSV to CSV and Preserve Embedded Commas. Preserve field values with explicit parsing, encoding and a checked output round trip.
To convert TSV to CSV, read each tab-delimited record with its actual quoting rules and write the fields through a CSV writer. The comma-separated output must quote a field containing a comma. Replacing tabs with commas directly can turn one field into several columns.
Inspect the TSV contract
This example expects UTF-8 text, an optional byte-order mark, a header row and quote-aware tab-delimited records. A text file using literal backslash-t sequences is not the same as a file containing tab characters. Inspect a small sample before choosing the delimiter.
A row with id 00123, name Lee, Ana and an empty note should become a three-column CSV record. The name is quoted in the output so its internal comma stays inside one field. The empty final field should remain present rather than being lost when a line is trimmed carelessly.
Check values through the output parser
The final assertion reopens the CSV and compares every parsed field with the TSV input. The raw files differ because their delimiters and quoting differ, so a byte-for-byte comparison is not the right success criterion. Parsed values, column order and record count are the relevant contract.
For a second fixture, change the identifier and put a quotation mark inside the note. Confirm that the output parser returns the new values and that doubled quote escaping has not become part of the value itself. A visually plausible text preview cannot establish that distinction.
Preserve text during spreadsheet import
A CSV file does not specify a spreadsheet column type. When opening the result in a spreadsheet, import the identifier column as text if its leading zeros matter. The converter preserves the string 00123, but a later automatic import can still turn it into the number 123.
Keep that downstream step separate when troubleshooting. If the CSV parser returns the correct identifier and the spreadsheet does not, investigate the spreadsheet’s import typing rather than rewriting a correct CSV export.
Executable conversion example
import csv
with open("input.tsv", encoding="utf-8-sig", newline="") as source:
rows = list(csv.reader(source, delimiter=" ", strict=True))
if not rows or not rows[0]:
raise ValueError("Expected a header row")
if any(len(row) != len(rows[0]) for row in rows):
raise ValueError("Every record must have the same number of fields")
with open("output.csv", "w", encoding="utf-8", newline="") as target:
csv.writer(target, delimiter=",", lineterminator="\n").writerows(rows)
with open("output.csv", encoding="utf-8", newline="") as check:
assert list(csv.reader(check, delimiter=",", strict=True)) == rows
Use a representative acceptance fixture
Begin with two different data records containing a leading-zero identifier, an embedded comma, a quotation mark, an empty field and a non-ASCII name when the destination supports it. Read the output with a parser for the destination format and compare the actual values, column order and record count. A save message or familiar extension does not establish that the records survived correctly.
Change the second fixture’s values rather than rerunning only the same sample. Include a malformed record and confirm that the conversion reports it instead of producing a misleading success. Keep the original unchanged and write the result to a separate path. Avoid exposing sensitive source values in public error logs.
Choose the format-aware operation
The Convert Text to CSV tool here works with already extracted plain text. It does not infer every structured file format or replace the Python parsers shown above. Use the operation whose input contract matches the actual source, and verify the result independently before importing it into another application.
Python reference
Python’s CSV module documentation explains the parser and writer dialect options. The published snippets and acceptance cases in this family are executed locally against disposable fixtures.
Verify the first record’s role
Confirm that the first record really contains the intended column headings. Consistent field counts cannot prove that a file has the right header names. Compare those names with the receiving schema before import, and do not silently use a data row as a substitute for missing headings.


