csv.QUOTE_MINIMAL
QUOTE_MINIMAL (the default) quotes only fields that need it. The others quote everything, everything but numbers, nothing, only str values, or everything but None — and three of them also change what the reader returns.
Demo
import csv, io buf = io.StringIO(newline='') csv.writer(buf, quoting=0, escapechar='\\').writerow(['Ada', 36, 2.5, None, '']) buf.getvalue()
Writing: None and '' both become empty fields; QUOTE_STRINGS and QUOTE_NOTNULL quote '' but not None, so the difference survives the file. Reading: QUOTE_NONNUMERIC converts every unquoted, non-empty field with float() — "Ada" raises ValueError: could not convert string to float: 'Ada'. QUOTE_STRINGS converts too, and also turns an empty unquoted field into None; QUOTE_NOTNULL only does the None part. QUOTE_NONE treats quote characters as ordinary text.
Common patterns
import csv with open('out.csv', 'w', newline='', encoding='utf-8') as f: csv.writer(f, quoting=csv.QUOTE_ALL).writerows(rows)
import csv with open('out.csv', 'w', newline='', encoding='utf-8') as f: csv.writer(f, quoting=csv.QUOTE_NOTNULL).writerows(rows) with open('out.csv', newline='', encoding='utf-8') as f: rows = list(csv.reader(f, quoting=csv.QUOTE_NOTNULL))
import csv with open('nums.csv', newline='', encoding='utf-8') as f: rows = list(csv.reader(f, quoting=csv.QUOTE_NONNUMERIC))
Examples
Pitfalls
import csv list(csv.reader(['name,age', 'Ada,36'], quoting=csv.QUOTE_NONNUMERIC))
import csv rows = csv.reader(['name,age', 'Ada,36']) header = next(rows) [(name, int(age)) for name, age in rows]
import csv next(csv.reader(['"1",2'], quoting=csv.QUOTE_ALL))
import csv next(csv.reader(['"1",2'], quoting=csv.QUOTE_NONNUMERIC))
When to use
- QUOTE_ALL / QUOTE_NONNUMERIC: the consumer expects quoted text
- QUOTE_NOTNULL / QUOTE_STRINGS: None must survive a round trip (3.12+)
- QUOTE_NONE + escapechar: formats that use backslash escapes instead of quotes
- QUOTE_NONNUMERIC for reading files you did not write that way
Notes
FAQ
Pass quoting=csv.QUOTE_ALL to csv.writer (or DictWriter). Every field, numbers included, is wrapped in quotes.