Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19517

Re: write 中文 into c:\t1

Date 2012-01-26 22:01 -0500
From Dave Angel <d@davea.name>
Subject Re: write 中文 into c:\t1
References <CA+YdQ_4SdtzD_vk4uUG37mOTHKi0r+_aJJXy89wvsJFbr1x2_Q@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.5150.1327633327.27778.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: write 中文 into c:\t1 Dave Angel <d@davea.name> - 2012-01-26 22:01 -0500

csiph-web