On this page
csv.reader
Prev: csv.list_dialects | Up: csv | Next: csv.register_dialect
Rel: csv.Dialect, csv.DictReader, csv.DictWriter, csv.Error
Ref: docs.python.org — csv.reader
Tags: #public #python3 #stdlib #csv
Import
import csv
from csv import reader
Signature & Overview
csv.reader(iterable, /, dialect='excel', **fmtparams)
Return a reader object that will process lines from the given iterable.
Usage
import csv
import io
rows = list(csv.reader(io.StringIO('id,note\n1,"hi, there"\n')))
assert rows == [["id", "note"], ["1", "hi, there"]]
Gotchas
- Always open files with
newline=""andencoding="utf-8": Required so embedded newlines inside quoted fields parse properly. - Inspect
r.line_num: Tracks the physical source line number for error messages.
REPL Inspect
from csv import reader
help(reader)
Linked from
Related notes
- tuts/python3/stdlib_module/csv/list_dialects Linked here; links to this page; shared tags: #csv, #stdlib
- tuts/python3/stdlib_module/csv/register_dialect 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/Dialect Linked here; shared tags: #csv, #stdlib
- tuts/python3/stdlib_module/csv/DictReader Linked here; shared tags: #csv, #stdlib
- tuts/python3/stdlib_module/csv/DictWriter Linked here; shared tags: #csv, #stdlib
- tuts/python3/stdlib_module/csv/Error Linked here; shared tags: #csv, #stdlib
- tuts/python3/stdlib_module/csv/excel shared tags: #csv, #stdlib
- tuts/python3/stdlib_module/csv/excel_tab shared tags: #csv, #stdlib
- tuts/python3/stdlib_module/csv/field_size_limit shared tags: #csv, #stdlib