Dev.to WebDev 🛠 Dev 👁 1 📖 1 min read

How Do You Put a Quote Inside a CSV Field?

A CSV field is wrapped in quotes to protect it. So what happens when the field itself contains a quote? I tested it with a real run, and the answer is one rule: write two quotes in a row. Full run in this Short: https://

A CSV field is wrapped in quotes to protect it. So what happens when the field itself contains a quote? I tested it with a real run, and the answer is one rule: write two quotes in a row. Full run in this Short: https://youtu.be/J_7vSa_Ir48

Two quotes in a row mean one

Inside a quoted field, a doubled quote is an escaped quote. This:

"He said ""hi"""

is one field whose value is He said "hi". The parser reads the two quotes as a single literal quote and keeps going.

The unterminated quote is an error

A quoted field that never closes is malformed, and the parser should say so. Mine throws an error on an unterminated quote instead of guessing where the field ends. Silently guessing is how bad data slips through.

Both behaviors come from the same small state machine as the comma-in-quotes case: track whether you are inside quotes, and treat a doubled quote as an escape.

What I tested, and what I did not

Tested with 8 real test cases, covering doubled quotes and unterminated quotes. Not tested: huge files and encodings other than UTF-8. For production, use a maintained library.

Watch the run

The real terminal output is in the Short: https://youtu.be/J_7vSa_Ir48 (narration is AI-generated; the test output is from a real run). The full build, a 17-line parser, is here: https://youtu.be/zdCNzOnxUv4

Subscribe to Ship Stack AI on YouTube and tell me in the comments what to build and break next.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.