write 中文 into c:\t1

Dave Angel d at davea.name
Thu Jan 26 22:01:12 EST 2012


On 01/26/2012 09:46 PM, contro opinion wrote:
>>>> s='\xd6\xd0\xce\xc4'
>>>> print s
>>>> ÖÐÎÄ
>>>> s1=s.decode('gbk').encode('utf-8')
>>>> print s1
>>>> 中文
>>>> file=open('c:\\t1','w')
>>>> file.write(s1)
>>>> file.close()
> when i open c:\t1,i get ÖÐÎÄ in it,
> how can i write  中文 into  c:\t1??

You did, encoded as utf-8
>>>> file.write(print s1)
You're using python 2, so how could you think that you could use a
keyword 'print' inside the expression?
>   File "<stdin>", line 1
>     file.write(print s1)
>                    ^
> SyntaxError: invalid syntax
>
So you've written the file correctly, in utf8. What makes you think you
haven't? Now how do you read it? Are you using a text viewer that will
let you specify the encoding of the file as you open it? Or that can be
set to default to utf-8?

Try reading it back in, and printing it. Apparently your stdout is
defined as utf-8 by default (since you liked the print you got earlier),
so you shouldn't have to decode the file if you just want to examine it
by printing it.



-- 

DaveA




More information about the Python-list mailing list