Unicode output to file

Евгений Косенко eugine_kosenko at ukr.net
Tue Dec 2 13:35:46 EST 2003


В сообщении от 1 Декабрь 2003 03:43 Philippe Mougin написал(a):

> #!/usr/bin/python
> # -*- coding: cp855 -*-

Big thanks for the help. However, this is not the core of the problem. Really, 
it is enough to use only the second line (my previous failures were due to my 
misunderstanding of the line syntax). The solution removes the deprecated 
message, but the problem is remaining:

Traceback (most recent call last):
  File "xp.py", line 9, in ?
    f.write(u"Привет")
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-5: 
ordinal not in range(128)

Note, it seems Python tempts to recode the unicode string in a 8-bit character 
string, and fails because the characters are outside of the lowest part of 
the unicode table.

First note: what is the problem to convert the characters, if all of them 
belong to the same codepage (btw, I use cp1251)?

Second note: I merely need to save the string in the native utf8 format. I 
DON'T need to convert it in any format. If I would want that, I will not use 
unicode at all, it is enough to use native 8-bit format.

The main question is "How to switch off the recoding?"

Well, I've tried to use utf-8 in the suggested metacomment:

# -*- coding: utf-8 -*-

f = file(u"content.new", u"wb")
print f.encoding
f.write(u"Привет")

The result is

UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-1: invalid 
data

Where is the problem now and how to remove it?





More information about the Python-list mailing list