csv.excel
Readers and writers use excel unless told otherwise. excel_tab only changes the delimiter to a tab; unix_dialect ends lines with \n and quotes every field. Class names use underscores, registry names use hyphens: excel_tab is 'excel-tab', unix_dialect is 'unix'.
Demo
import csv, io buf = io.StringIO(newline='') w = csv.writer(buf, dialect='excel') w.writerows([['id', 'note'], [1, 'say "hi"']]) buf.getvalue()
unix quotes every field, including the number 1, and ends lines with \n; excel and excel-tab end them with \r\n. The class name excel_tab is not a registered name, so dialect='excel_tab' raises csv.Error: unknown dialect — pass the class itself (dialect=csv.excel_tab) or the hyphenated name. Reading comma text as excel-tab gives one field per line, quotes included, because the quote does not start the field.
Common patterns
import csv with open('data.tsv', newline='', encoding='utf-8') as f: rows = list(csv.reader(f, dialect='excel-tab'))
import csv with open('out.csv', 'w', newline='', encoding='utf-8') as f: csv.writer(f, dialect='unix').writerows(rows)
import csv class SemicolonExcel(csv.excel): delimiter = ';'
Examples
Pitfalls
import csv, io csv.writer(io.StringIO(), dialect='unix_dialect')
import csv, io buf = io.StringIO(newline='') csv.writer(buf, dialect='unix').writerow(['a', 1]) buf.getvalue()
import csv next(csv.reader(['Name;Price', 'Tea;2,50'], dialect='excel'))
import csv next(csv.reader(['Name;Price', 'Tea;2,50'], delimiter=';'))
When to use
- excel: the default — files for and from spreadsheets
- excel-tab: TSV files
- unix: tools expecting \n line endings and quoted fields
- Semicolon CSV from European Excel → delimiter=";" (or your own dialect)
Notes
FAQ
excel: comma delimiter, double-quote quotechar, doubled quotes inside fields, minimal quoting and \r\n line endings.