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

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue May 29 13:31:37 EDT 2007


En Tue, 29 May 2007 04:24:15 -0300, 人言落日是天涯,望极天涯不见家  
<kelvin.you at gmail.com> escribió:

> On 5月29日, 下午3时05分, "Martin v. Lo"wis" <mar... at v.loewis.de> 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)
>>
> 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)

Just to provide an example of what MvL already said:

py> line = u"Delta=\u0394"
py> f = open("data.txt","w")
py> f.write(line.encode("utf8"))
py> f.close()
py> print repr(open("data.txt").read())
'Delta=\xce\x94'

py> import codecs
py> f = codecs.open("data2.txt","w","utf8")
py> f.write(line)
py> f.close()
py> print repr(open("data2.txt").read())
'Delta=\xce\x94'

-- 
Gabriel Genellina




More information about the Python-list mailing list