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
-- http://mail.python.org/mailman/listinfo/python-list