csv.reader
Read lines in CSV format and split each one into values.
Python 3.13
csv.reader(csvfile, dialect='excel', **fmtparams)- =default value
- csvfile
- A file open for reading with newline='', io.StringIO or any iterable of strings.Iterable[str]
- dialect
- A set of format settings; 'excel' by default, which is comma-separated CSV.str | DialectOptional'excel''excel-tab''unix'
- **fmtparams
- Individual settings, such as delimiter=';' for the separator or quotechar for the quote character.Any
Return value
An iterator of rows: each row is a list of string values.
Examples
Reading CSV row by row
Python 3.13
import csv
import io
text = io.StringIO('name,age\nAnna,25\nBoris,31\n')
for row in csv.reader(text):
print(row)['name', 'age'] ['Anna', '25'] ['Boris', '31']
The ; separator and a quoted value
Python 3.13
import csv
import io
text = io.StringIO('city;comment\nMoscow;"big; busy"\n')
rows = list(csv.reader(text, delimiter=';'))
print(rows[1])['Moscow', 'big; busy']
Details
All values come back as strings, so convert numbers yourself: int(row[1]). Open the file with newline='' so that line breaks inside quotes are read correctly. Skip the header row with next(reader), and read rows as dictionaries with csv.DictReader. For tab-separated values, there is the 'excel-tab' dialect.
