csv.DictReader and unicode

L

Laszlo Nagy

This program

fin = codecs.open(fname,"r",encoding="UTF-8")
eader = csv.DictReader(fin)
for values in reader:
pass

results in:

File "run.py", line 23, in process_file
for values in reader:
File "/usr/local/lib/python2.5/csv.py", line 83, in next
row = self.reader.next()
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in
position 13: ordinal not in range(128)

As you can see the exception is thrown in csv.py. How it is possible?
The csv.DictReader should not use ascii codec for anything, because the
file encoding is UTF-8.

Please help.

Best,

Laszlo
 
P

Peter Otten

Laszlo said:
This program

fin = codecs.open(fname,"r",encoding="UTF-8")
eader = csv.DictReader(fin)
for values in reader:
pass

results in:

File "run.py", line 23, in process_file
for values in reader:
File "/usr/local/lib/python2.5/csv.py", line 83, in next
row = self.reader.next()
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in
position 13: ordinal not in range(128)

As you can see the exception is thrown in csv.py. How it is possible?
The csv.DictReader should not use ascii codec for anything, because the
file encoding is UTF-8.

The csv module doesn't support unicode. Read the values as byte strings and
decode afterwards.

Peter
 
L

Laszlo Nagy

Peter said:
Laszlo Nagy wrote:



The csv module doesn't support unicode.
I understand that csv does not support unicode. I figured out that the
exception will not be thrown if I open the file with the built in open()
call.
Read the values as byte strings and decode afterwards.
Is there a plan to make csv reader compatible with unicode?

Thanks,

Laszlo
 
P

Peter Otten

Or monkey-patch:

import csv

def make_reader(fin, encoding="UTF-8"):
reader = csv.DictReader(fin)
reader.reader = ([col.decode(encoding) for col in row] for row in reader.reader)
return reader

fin = open("example.csv")
for record in make_reader(fin):
print record
Is there a plan to make csv reader compatible with unicode?

I don't know.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top