Many exports aren't comma-separated at all: European Excel uses semicolons, and logs often use tabs.
"CSV" is a family of formats. The csv module describes a format as a dialect: a set of formatting parameters.
delimiter: field separator, default",". Use"\t"for TSV,";"for many European spreadsheet exports,"|"for some database dumps.quotechar: default'"'.lineterminator: used by the writer only, default"\r\n"; the reader accepts\r,\nor\r\nregardless.skipinitialspace=True: ignore spaces after the delimiter, for files likea, b, c.doublequote,escapechar,quoting,strict: covered in the quoting lesson.
Pass parameters directly, csv.reader(f, delimiter=";"), or name a dialect: built-ins are "excel" (the default), "excel-tab" and "unix" (which ends lines with \n and quotes all fields).
To reuse settings, register your own: csv.register_dialect("pipes", delimiter="|", quoting=csv.QUOTE_NONE, escapechar="\\"), then csv.reader(f, dialect="pipes"). You can also subclass csv.Dialect. csv.list_dialects() shows what's registered. Keyword arguments override the dialect's values, so dialect="excel", delimiter=";" works.
Example
import csv, io
tsv = "name\tscore\nAda\t9.5\n"
print(list(csv.reader(io.StringIO(tsv), dialect="excel-tab")))
# [['name', 'score'], ['Ada', '9.5']]
euro = "name;price\nTea;3,50\n"
rows = list(csv.reader(io.StringIO(euro), delimiter=";"))
print(rows[1], float(rows[1][1].replace(",", "."))) # ['Tea', '3,50'] 3.5
print(list(csv.reader(io.StringIO("a, b, c"), skipinitialspace=True))) # [['a', 'b', 'c']]
csv.register_dialect("pipes", delimiter="|", lineterminator="\n")
buf = io.StringIO()
csv.writer(buf, dialect="pipes").writerows([["id", "msg"], [1, "a|b"]])
print(buf.getvalue(), end="")
# id|msg
# 1|"a|b"
print(csv.list_dialects()) # ['excel', 'excel-tab', 'unix', 'pipes'] (order may vary)Remember
- delimiter, quotechar and lineterminator define the format
- Built-in dialects: excel, excel-tab, unix
- register_dialect names a reusable set of options
Watch out for
- Setting lineterminator for the reader and expecting it to matter; it only affects writing.