CSV vs TSV: Nên Chọn Định dạng Phân tách Nào?
Quick Answer
Dùng CSV cho trao đổi dữ liệu chung, import/export bảng tính và tương thích tối đa. Dùng TSV khi dữ liệu chứa dấu phẩy nhưng không chứa tab, để tránh quoting và escaping. TSV phổ biến trong bioinformatics và NLP; CSV là default phổ quát.
Introduction
CSV (Comma-Separated Values) và TSV (Tab-Separated Values) là hai định dạng plain-text cho dữ liệu bảng. Cả hai lưu hàng as dòng với trường phân tách bởi dấu phân cách. CSV dùng dấu phẩy; TSV dùng ký tự tab. Việc chọn dấu phân cách ảnh hưởng đến escaping, tương thích với bảng tính và xử lý trường văn bản chứa dấu phân cách.
Step by Step
-
Compare delimiter and escaping
CSV uses commas. If a field contains a comma, it must be wrapped in quotes: "Smith, John". If a field contains quotes, they must be escaped: "He said ""hello""". TSV uses tabs. Tabs rarely appear in text data, so TSV fields usually need no quoting or escaping — simpler parsing.
-
Compare compatibility
CSV is universally supported: Excel, Google Sheets=Sheets, every database, every programming language. TSV is also widely supported but less standardized — some tools interpret tabs differently. Excel auto-detects both, but CSV is the default export format.
-
Compare parsing complexity
CSV parsing requires a state machine to handle quoting rules (RFC 4180). TSV parsing is trivial: split each line by tab character. This makes TSV faster to parse and less error-prone. However, CSV has a formal spec (RFC 4180); TSV has no formal standard.
-
Compare use cases
CSV: spreadsheet import/export, data APIs, ETL pipelines, config files, universal data exchange. TSV: bioinformatics (BLAST, GFF), NLP (CoNLL format), clipboard data (copy from Excel = TSV), log files with tab delimiters.
-
Decide based on your data
If your data contains commas (names, addresses, sentences): TSV avoids quoting. If your data contains tabs (rare in text): CSV avoids quoting. If you need maximum compatibility: CSV. If you want simpler parsing and your data is tab-free: TSV.
Examples
Same data in CSV and TSV
Input: CSV: name,age,city\n"Smith, John",30,"New York, NY"\nTSV: name\tage\tcity\nSmith, John\t30\tNew York, NY
Output: CSV requires quotes for fields with commas. TSV needs no quoting because tabs don't appear in the data.
Common Problems
- CSV quoting edge cases: fields containing commas, quotes, or newlines require RFC 4180 quoting rules — a naive split by comma will corrupt data with embedded commas.
- TSV has no formal standard: unlike CSV (RFC 4180), TSV has no official spec, so tools may handle edge cases differently — use TSV only within controlled pipelines.
- Excel encoding issues: Excel may export CSV with BOM or locale-specific delimiters (semicolon in EU locales) — specify UTF-8 encoding and verify delimiter on import.
- Newline ambiguity: CSV fields with embedded newlines are valid but break line-by-line parsing — TSV rarely has this problem because tabs are uncommon in text data.
Tips
- Use CSV for maximum compatibility — every spreadsheet, database, and programming language supports it natively.
- Use TSV when your data contains commas but not tabs (names, addresses, sentences) to avoid quoting complexity entirely.
- Always use a proper CSV parser (not string.split) to handle RFC 4180 quoting rules — naive splitting corrupts fields with embedded commas or quotes.
- When copying data from Excel to a text editor, the clipboard uses TSV format — paste into a .tsv file to preserve fields with commas correctly.