I have a latin1 encoded file. How would I do the equivalent of the following with csv
?
>>> import csv
>>> with open(filepath, 'rb') as csvfile:
... reader = csv.DictReader(csvfile, delimiter='\t', encoding='iso-8859-1')
I have a latin1 encoded file. How would I do the equivalent of the following with csv
?
>>> import csv
>>> with open(filepath, 'rb') as csvfile:
... reader = csv.DictReader(csvfile, delimiter='\t', encoding='iso-8859-1')
with open(filepath, "r", encoding="ISO-8859-1") as csvfile:
reader = csv.DictReader(csvfile)
Here is a way you can do it:
def Latin1ToUnicodeDictReader(latin1_data, **kwargs):
csv_reader = csv.DictReader(latin1_data, **kwargs)
for row in csv_reader:
yield {key: value.decode('iso-8859-1').encode('utf8') if value else value for key, value in row.iteritems()}
reader = Latin1ToUnicodeDictReader(csvfile, delimiter='\t')