how to print the GREEK CAPITAL LETTER delta under utf-8 encoding

"Martin v. Lo"wis" martin at v.loewis.de
Tue May 29 03:05:14 EDT 2007


> yes, it could print to the terminal(cmd.exe), but when I write these
> string to file. I got the follow error:
> 
>   File "E:\Tools\filegen\filegen.py", line 212, in write
>     self.file.write(data)
> UnicodeEncodeError: 'ascii' codec can't encode character u'\u0394' in
> position 0
> : ordinal not in range(128)

Yes, when writing to a file, you need to define an encoding, e.g.

self.file.write(data.encode("utf-8"))

You can use codecs.open() instead of open(),
so that you can just use self.file.write(data)

Alternatively, you can find out what sys.stdout.encoding is,
and use that when encoding data for the terminal (falling back
to "utf-8" when .encoding is not available on the file).

> but other text, in which include "chinese characters" got from
> os.listdir(...),  are written to the file OK. why?

Your version of Windows uses a code page that supports Chinese
characters in the byte-oriented character set. These are normally
encoded using the "mbcs" encoding (except that the terminal likely
uses a different encoding). So if you use "mbcs" instead of "utf-8",
you might be able to read the text as well.

Regards,
Martin



More information about the Python-list mailing list