Skip to content Skip to sidebar Skip to footer

Trouble With Unicodecsv Reader In Python

I'm having trouble using the unicodecsv reader. I keep looking for different examples of how to use the module, but everyone keeps referencing the exact sample from the unicodecsv

Solution 1:

You need to declare the encoding of the input file when creating the reader, just like you did when creating the writer:

>>> import unicodecsv as csv
>>> withopen('example.csv', 'wb') as f:
...     writer = csv.writer(f, encoding='utf-8')
...     writer.writerow(('heading0', 'heading1'))
...     writer.writerow((u'é', u'ñ'))
...     writer.writerow((u'ŋ', u'ŧ'))
... >>> withopen('example.csv', 'rb') as f:
...     reader = csv.reader(f, encoding='utf-8')
...     headers = next(reader)
... print headers
...     data = {i: v for (i, v) inenumerate(reader)}
... print data
... 
[u'heading0', u'heading1']
{0: [u'\xe9', u'\xf1'], 1: [u'\u014b', u'\u0167']}

Printing the dictionary shows the escaped representation of the data, but you can see the characters by printing them individually:

>>>for v in data.values():...for s in v:...print s... 
é
ñ
ŋ
ŧ

EDIT:

If the encoding of the file is unknown, then it's best to use some like chardet to determine the encoding before processing.

Solution 2:

If your final goal is read csv file and convert data into dicts then I would recommend using csv.DictReader. DictRead will take care of reading header and converting rest of the rows into Dict (rowdicts). This uses CSV moduels, which contains lots of documentation/example available.

>>>import csv>>>withopen('names.csv') as csvfile:...    reader = csv.DictReader(csvfile)...for row in reader:...print(row['first_name'], row['last_name'])

To get more clarity you check examples here https://docs.python.org/2/library/csv.html#csv.DictReader

Post a Comment for "Trouble With Unicodecsv Reader In Python"