Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #19517 > unrolled thread
| Started by | Dave Angel <d@davea.name> |
|---|---|
| First post | 2012-01-26 22:01 -0500 |
| Last post | 2012-01-26 22:01 -0500 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: write 中文 into c:\t1 Dave Angel <d@davea.name> - 2012-01-26 22:01 -0500
| From | Dave Angel <d@davea.name> |
|---|---|
| Date | 2012-01-26 22:01 -0500 |
| Subject | Re: write 中文 into c:\t1 |
| Message-ID | <mailman.5150.1327633327.27778.python-list@python.org> |
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 top | Article view | comp.lang.python
csiph-web