Unicode output to file

  • Thread starter =?windows-1251?b?xeLj5e3o6SDK7vHl7eru?=
  • Start date
?

=?windows-1251?b?xeLj5e3o6SDK7vHl7eru?=

Hi!

The next program

f = file("content.new", "wb")
print f.encoding
f.write(u"Ïðèâåò")

Gives the next results:

sys:1: DeprecationWarning: Non-ASCII character '\xcf' in file xp.py on line
10, but no encoding declared; see http://www.python.org/peps/pep-0263.html
for details
None
Traceback (most recent call last):
File "xp.py", line 10, in ?
f.write(u"Ïðèâåò")
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-5:
ordinal not in range(128)

I use here Russian letters, and need special encoding for them. The property
'encoding' is read-only, and is None by default. How to cast the encoding for
a file?
 
M

Martin v. =?iso-8859-15?q?L=F6wis?=

=?windows-1251?b?xeLj5e3o6SDK7vHl7eru?= said:
I use here Russian letters, and need special encoding for them. The
property 'encoding' is read-only, and is None by default. How to
cast the encoding for a file?

You should use codecs.open instead of the builtin open function; this
allows you to pass an encoding for the file stream.

HTH,
Martin
 
G

Guest

Ð’ Ñообщении от 1 Декабрь 2003 08:53 Martin v. Löwis напиÑал(a):
You should use codecs.open instead of the builtin open function; this
allows you to pass an encoding for the file stream.

Big thanks! This just helped me!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top