Skip to main content

Lib/csv.py

cpython 3.14 @ ab2d84fe1023/Lib/csv.py

Lib/csv.py re-exports the C types from _csv and adds DictReader, DictWriter, and Sniffer. The actual CSV tokeniser is _csv.reader / _csv.writer.

Map

LinesSymbolRole
1-30Re-exports from _csvreader, writer, Dialect, register_dialect, get_dialect
31-200DictReaderWraps reader; maps each row to a dict using fieldnames
201-300DictWriterWraps writer; serialises dicts using fieldnames order
301-380SnifferDetect delimiter and quote character from a sample

Reading

DictReader

DictReader takes a fieldnames parameter or infers field names from the first row. Each __next__ call returns an OrderedDict mapping field names to values. The restkey parameter stores extra fields; restval fills missing fields.

# CPython: Lib/csv.py:82 DictReader.__next__
def __next__(self):
...
row = next(self.reader)
...
d = dict(zip(self.fieldnames, row))
lf = len(self.fieldnames)
lr = len(row)
if lf < lr:
d[self.restkey] = row[lf:]
elif lf > lr:
for key in self.fieldnames[lr:]:
d[key] = self.restval
return d

Sniffer.sniff

Sniffer.sniff(sample) analyses a sample string and returns a Dialect subclass with detected delimiter and quotechar. It tries to count character frequency patterns to identify the most likely delimiter.

DictWriter.writeheader

DictWriter.writeheader() writes a row consisting of the fieldnames values. This convenience method was added in Python 2.7.

gopy notes

Not yet ported. Once module/csv/ lands with the _csv C backend equivalent, DictReader and DictWriter can be ported as thin Go structs that wrap the Go csv.Reader / csv.Writer. Sniffer is optional for the initial port.