Skip to content
Writing CSV
step 1/5

Reading — step 1 of 5

Read

~1 min readEdge Cases

Writing CSV

Writing CSV is the inverse: take rows of strings and emit valid CSV. The trick is quoting:

python

Decisions:

  • Always quote vs quote when needed: always-quote produces consistent output but bigger files. Excel uses always-quote-strings.
  • Line ending: \r\n (Windows-style) is RFC 4180 spec. \n is more common today.
  • Encoding: UTF-8 with BOM for Excel compatibility (encoding='utf-8-sig').

Common bugs:

  • Forgetting to escape quotes inside quoted fields → corrupted file
  • Not quoting fields with newlines → row count mismatch
  • Mixed line endings inside the file → some readers see different row counts

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…