Unicode output to file

Philippe Mougin firi_mu at fjcl.fujitsu.com
Mon Dec 1 02:43:17 CET 2003


Hi Eugine,

As is stated in the PEP page given in the error message, you need to add
these 2 comment lines at the beginning of your source code is using an
encoding different from ASCII (starting from Python 2.3):

#!/usr/bin/python
# -*- coding: <encoding name> -*-

So in your case, I presume you should use something like :

#!/usr/bin/python
# -*- coding: cp855 -*-
f = file("content.new", "wb")
...

Cheers,

Philippe. 

> Hi!
> 
> The next program
> 
> f = file("content.new", "wb")
> print f.encoding
> f.write(u"マph粢r")
> 
> 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"マph粢r")
> 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?
> 






More information about the Python-list mailing list