Recording the Way

CSV Files

2018/01/14

csv.reader(csvfile, dialect=’excel’, *\fmtparams*)

If csvfile is a file object, it should be opened with newline=''.

A short usage example:

1
2
3
4
5
import csv
with open('eggs.csv', newline='') as csvfile:
spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
for row in spamreader:
print(', '.join(row))

csv.writer(csvfile, dialect=’excel’, *\fmtparams*)

If csvfile is a file object, it should be opened withnewline=''

1
2
3
4
5
6
import csv
with open('eggs.csv', 'w', newline='') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=' ',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
CATALOG
  1. 1. csv.reader(csvfile, dialect=’excel’, *\fmtparams*)
  2. 2. csv.writer(csvfile, dialect=’excel’, *\fmtparams*)