On 5月29日, 下午3时05分, "Martin v. Lo"wis" <[EMAIL PROTECTED]> wrote: > > 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
Thanks a lot! I want to just use the utf-8. how could I convert my 'mbcs' encoding to the utf-8 and write it to the file? I have replaced the open() to codecs.open() but it still can not decode the 'mbcs', the error is as follow: File "E:\Tools\filegen\filegen.py", line 213, in write self.file.write(data) File "C:\Python25\lib\codecs.py", line 638, in write return self.writer.write(data) File "C:\Python25\lib\codecs.py", line 303, in write data, consumed = self.encode(object, self.errors) UnicodeDecodeError: 'ascii' codec can't decode byte 0xcc in position 32: ordinal not in range(128)
-- http://mail.python.org/mailman/listinfo/python-list