Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: write 中文 into c:\t1

2 views
Skip to first unread message

MRAB

unread,
Jan 26, 2012, 9:53:18 PM1/26/12
to pytho...@python.org
On 27/01/2012 02:46, 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??
>
> >>> file.write(print s1)
> File "<stdin>", line 1
> file.write(print s1)
> ^
> SyntaxError: invalid syntax
>
file.write(s.decode('gbk').encode('utf-8'))

Dave Angel

unread,
Jan 26, 2012, 10:01:12 PM1/26/12
to contro opinion, python-list
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

0 new messages