Python Csv
csv is a module that supports (de-)serializing CSV files.
Contents
Usage
DictReader
DictWriter
Reader
import csv
with open(file, 'r', newline='', encoding='latin-1') as f:
r = csv.reader(f)
for row in r:
print(list(row))
Quote_All
Passing the constant csv.QUOTE_ALL as the quoting option on a CSV writer instructs the formatter to quote all fields using the quotechar option's character.
Quote_Minimal
Passing the constant csv.QUOTE_MINIMAL as the quoting option on a CSV writer instructs the formatter to quote fields which contain special characters using the quotechar option's character.
Special characters include all characters set in the delimiter, quotechar, and lineterminator options.
Quote_NonNumeric
Passing the constant csv.QUOTE_NONNUMERIC as the quoting option on a CSV writer instructs the formatter to quote all non-numeric fields using the quotechar option's character.
Passing as the quoting option on a CSV reader instructs the parser to convert all fields not quoted by the quotechar option's character into floats.
Quote_None
Passing the constant csv.QUOTE_NONE as the quoting option on a CSV writer instructs the formatter to never quote fields. If the delimiter options's character is encountered in output, it is escaped with the escapechar options's character. If the escapechar option is not set when it is required, csv.Error is raised.
Passing as the quoting option on a CSV reader instructs the parser to retain the quotechar option's character in data.
Writer
import csv
data = [["John", "Doe", 123], ["Jane", "Doe", 456]]
with open(file, 'w', newline='', encoding='latin-1') as f:
w = csv.writer(f)
w.writerows(data)Alternatively, write individual rows.
for row in data:
w.writerow(data)
See also
Python csv module documentation
Python Module of the Day article for csv
