csv reader

Chris Rebert clp2 at rebertia.com
Tue Dec 15 16:33:31 EST 2009


On Tue, Dec 15, 2009 at 1:24 PM, Emmanuel <manouchk at gmail.com> wrote:
> I have a problem with csv.reader from the library csv. I'm not able to
> import accentuated caracters. For example, I'm trying to import a
> simple file containing a single word "equação" using the following
> code:
>
> import csv
> arquivoCSV='test'
> a=csv.reader(open(arquivoCSV),delimiter=',')
> tab=[]
> for row in a:
>    tab.append(row)
> print tab
>
> As a result, I get:
>
> [['equa\xe7\xe3o']]
>
> How can I solve this problem?

>From http://docs.python.org/library/csv.html :
"""
Note:
This version of the csv module doesn’t support Unicode input. Also,
there are currently some issues regarding ASCII NUL characters.
Accordingly, all input should be UTF-8 or printable ASCII to be safe;
see the examples in section Examples. These restrictions will be
removed in the future.
"""

Thus, you'll have to decode the results into Unicode manually; this
will require knowing what encoding your file is using. Files in some
encodings may not parse correctly due to the aforementioned NUL
problem.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list