Handle Escaped Quotes in Field Values
Learn how double quotes within field values are escaped during TSV to CSV conversion using the RFC 4180 double-quote escaping method.
Detailed Explanation
Escaped Quotes in Fields
When a field value itself contains double quote characters, proper escaping is critical to avoid parsing errors. RFC 4180 specifies that double quotes inside a quoted field are escaped by doubling them.
Example TSV Input
Product Description Price
Widget The "best" widget 19.99
Gadget Say "hello" to the new gadget 49.99
Cable 6" USB-C cable 12.99
Generated CSV Output
Product,Description,Price
Widget,"The ""best"" widget",19.99
Gadget,"Say ""hello"" to the new gadget",49.99
Cable,"6"" USB-C cable",12.99
How Quote Escaping Works
The escaping process follows these steps:
- Detection: The converter scans each field for quote characters
- Wrapping: If quotes are found, the entire field is wrapped in double quotes
- Escaping: Each internal double quote is replaced with two double quotes (
"")
Reading Escaped Quotes
When parsing CSV with escaped quotes, the process is reversed:
- A pair of double quotes (
"") inside a quoted field is read as a single literal double quote - A single double quote at the end of a field closes the quoted section
Common Pitfalls
Many naive CSV implementations fail to handle escaped quotes correctly, leading to:
- Fields being split at the wrong position
- Quote characters being lost or doubled incorrectly
- Entire rows being misaligned
This converter handles all edge cases correctly, including fields that start with a quote, end with a quote, or contain only quote characters.
Use Case
Converting product catalogs, user-generated content, or text data that contains quotation marks (such as measurements in inches, dialogue, or code snippets) between TSV and CSV formats.