On this page
csv.DictReader
Prev: csv.Dialect | Up: csv | Next: csv.DictWriter
Rel: csv.Dialect, csv.DictWriter, csv.Error, csv.excel
Ref: docs.python.org — csv.DictReader
Tags: #public #python3 #stdlib #csv
Import
import csv
from csv import DictReader
Signature & Overview
csv.DictReader(f, fieldnames=None, restkey=None, restval=None, dialect='excel', *args, **kwds)
csv.DictReader class.
Methods & Attributes
DictReader.fieldnames(...)—DictReader.fieldnamesmethod/property.
Usage
import csv
import io
text = "name,score\nAda,98\nGrace,100\n"
rows = list(csv.DictReader(io.StringIO(text)))
assert rows == [{"name": "Ada", "score": "98"}, {"name": "Grace", "score": "100"}]
Gotchas
- All values arrive as
str: Convert numeric columns (int(row["score"])) explicitly. - Extra/missing columns: Rows shorter than the header get
restval(defaultNone); extra columns are collected in a list underrestkey(defaultNone).
REPL Inspect
from csv import DictReader
help(DictReader)
Linked from
- tuts/python3/stdlib_module/csv
- tuts/python3/stdlib_module/csv/Dialect
- tuts/python3/stdlib_module/csv/DictWriter
- tuts/python3/stdlib_module/csv/Error
- tuts/python3/stdlib_module/csv/excel
- tuts/python3/stdlib_module/csv/excel_tab
- tuts/python3/stdlib_module/csv/field_size_limit
- tuts/python3/stdlib_module/csv/get_dialect
- tuts/python3/stdlib_module/csv/list_dialects
- tuts/python3/stdlib_module/csv/reader
- tuts/python3/stdlib_module/csv/register_dialect
- tuts/python3/stdlib_module/csv/Sniffer
- tuts/python3/stdlib_module/csv/unix_dialect
- tuts/python3/stdlib_module/csv/unregister_dialect
- tuts/python3/stdlib_module/csv/writer
Related notes
- tuts/python3/stdlib_module/csv/Dialect Linked here; links to this page; shared tags: #csv, #stdlib
- tuts/python3/stdlib_module/csv/DictWriter Linked here; links to this page; shared tags: #csv, #stdlib
- tuts/python3/stdlib_module/csv/Error Linked here; links to this page; shared tags: #csv, #stdlib
- tuts/python3/stdlib_module/csv/excel Linked here; links to this page; shared tags: #csv, #stdlib
- tuts/python3/stdlib_module/csv Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/csv/excel_tab links to this page; shared tags: #csv, #stdlib
- tuts/python3/stdlib_module/csv/field_size_limit links to this page; shared tags: #csv, #stdlib
- tuts/python3/stdlib_module/csv/get_dialect links to this page; shared tags: #csv, #stdlib
- tuts/python3/stdlib_module/csv/list_dialects links to this page; shared tags: #csv, #stdlib
- tuts/python3/stdlib_module/csv/reader links to this page; shared tags: #csv, #stdlib