Python csv Module
Example
Read CSV data from a string:
import csv
from io import StringIO
data = "name,age\nEmil,14\nTobias,12"
r = csv.reader(StringIO(data))
for row in r:
print(row)
Try it Yourself »
from io import StringIO
data = "name,age\nEmil,14\nTobias,12"
r = csv.reader(StringIO(data))
for row in r:
print(row)
Definition and Usage
The csv module reads and writes tabular data in CSV (Comma Separated Values) format.
Use it with dialects, quoting options, and convenience classes like DictReader and DictWriter.
Members
Member | Description |
---|---|
Dialect | Base class for dialect definitions. |
DictReader | Read CSV data into dictionaries, using the first row as fieldnames. |
DictWriter | Write dictionaries to CSV, using the keys as fieldnames. |
Error | Raised for CSV parsing or writing errors. |
excel | Dialect instance describing the typical Excel-generated CSV format. |
excel_tab | Dialect instance for tab-delimited files. |
field_size_limit() | Get or set the maximum allowed field size. |
get_dialect() | Return the dialect instance for a registered name. |
list_dialects() | Return a list of all registered dialect names. |
QUOTE_ALL | Quote all fields. |
QUOTE_MINIMAL | Quote fields containing special characters only. |
QUOTE_NONE | Disable quoting; use escapechar for special characters. |
QUOTE_NONNUMERIC | Quote all non-numeric fields. |
reader | Return a reader object that will iterate over lines in the CSV file. |
register_dialect() | Register a new dialect. |
unix_dialect | Dialect instance describing typical Unix-style CSV format. |
unregister_dialect() | Delete the named dialect. |
writer | Return a writer object responsible for converting data to CSV format. |
writerow() | Write a row to the CSV file. |
writerows() | Write multiple rows to the CSV file. |