Escape Quotes in CSV Files
In CSV files, fields that contain special characters, such as commas, newlines, or quotes, are enclosed in double quotes to prevent misparsing. This quoting ensures the parser reads the entire field correctly.
Key rules
- The escape quote is the double quote character:
". - To include a literal quote inside a field, write two quotes in a row:
"". This is the escape sequence. - Outer quotes are field delimiters, they mark the start and end of the field.
- Middle
""represents one literal"in the value.
Simple example
"Smith, John" is parsed as Smith, John because the outer quotes keep the comma inside the field.
Quoted quote example
Say you had a record with the text, John Smith said: “Hello”. It is escaped like this:
"John Smith said: ""Hello"""Here, the first and last " are field delimiters, and the two "" in the middle represent one literal quote. The parsed value becomes:
John Smith said: "Hello"Excel
Excel doesn’t show quotes when you copy/paste. It hides them from view but includes them in the clipboard.
Python
Python’s csv.reader (or similar) sees the real data, including the ” delimiters and escaped “” and prints them literally.
Excel Python Compare
| Source | What you see | What’s actually copied |
|---|---|---|
| Excel cell | Sum([Stock]) | "Sum([Stock])" |
| Clipboard | No quotes visible | Quotes are there |
Summary
Use quotes to wrap fields that contain commas, newlines, or quotes, and use "" inside those fields to represent a literal ". This preserves both structure and meaning for any CSV parser.