CSV เทียบกับ TSV: ควรเลือกรูปแบบตัวคั่นแบบไหน?
Quick Answer
ใช้ CSV สำหรับการแลกเปลี่ยนข้อมูลทั่วไป import/export สเปรดชีต และความเข้ากันได้สูงสุด ใช้ TSV เมื่อข้อมูลมีจุลภาคแต่ไม่มี tab เพื่อหลีกเลี่ยง quoting และ escaping TSV พบใน bioinformatics และ NLP; CSV เป็นค่าเริ่มต้นสากล
Introduction
CSV (Comma-Separated Values) และ TSV (Tab-Separated Values) เป็นรูปแบบ plain-text สำหรับข้อมูลตาราง ทั้งคู่เก็บแถวเป็นบรรทัด คั่นด้วยตัวคั่น CSV ใช้จุลภาค TSV ใช้ tab การเลือกตัวคั่นมีผลต่อ escaping ความเข้ากันได้กับสเปรดชีต และการจัดการฟิลด์ข้อความที่มีตัวคั่น
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.