Forgot to mention that I work on Windows XP and Windows 2003.

On 2/23/06, Rudy Schockaert <[EMAIL PROTECTED] > 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

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
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to