CSV module and UNICODE
Rudy Schockaert
rudy.schockaert at gmail.com
Thu Feb 23 05:56:28 EST 2006
Forgot to mention that I work on Windows XP and Windows 2003.
On 2/23/06, Rudy Schockaert <rudy.schockaert at gmail.com> wrote:
>
> I'm having problems writing unicode to a csv file.
> I use the following code:
>
> -------------------------
> import codecs
> import csv
>
> csvfile = csv.writer(codecs.open('filename.csv','w+','utf-8'))
> a = u'\xc9'
> csvfile.writerow([a,a])
> -------------------------
>
> It fails with the message: UnicodeEncodeError: 'ascii' codec can't encode
> .....
>
> Is there any way I can solve this.
>
> <PEP 305>
>
> 1.
>
> What about Unicode? Is it sufficient to pass a file object gotten
> from codecs.open()? For example:
>
> csvreader = csv.reader(codecs.open("some.csv", "r", "cp1252"))
>
> csvwriter = csv.writer(codecs.open("some.csv", "w", "utf-8"))
>
> In the first example, text would be assumed to be encoded as cp1252.
> Should the system be aggressive in converting to Unicode or should Unicode
> strings only be returned if necessary?
>
> In the second example, the file will take care of automatically
> encoding Unicode strings as utf-8 before writing to disk.
>
> Note: As of this writing, the csv module doesn't handle Unicode
> data.
>
> </PEP 305>
>
> PEP 305 was last modified on Wed, 11 Aug 2004
>
> <http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/python/python/nondist/peps/pep-0305.txt>
> Has something changed in between?
>
>
> Thanks in advance,
>
> Rudy
>
--
It is not economical to go to bed early to save the candles if the result is
twins. - Chinese Proverb
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060223/62e96ee1/attachment.html>
More information about the Python-list
mailing list