Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.022 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'encoded': 0.05; 'python': 0.08; '>>>>': 0.09; 'utf-8': 0.09; 'cc:addr:python-list': 0.15; 'syntax': 0.15; 'did,': 0.16; 'file.close()': 0.16; 'received:192.168.1.104': 0.16; 'stdout': 0.16; 'syntaxerror:': 0.16; 'wrote:': 0.16; 'written': 0.19; 'header:In-Reply-To:1': 0.22; "shouldn't": 0.23; 'defined': 0.24; 'keyword': 0.24; 'specify': 0.24; 'cc:2**0': 0.25; 'pm,': 0.26; 'invalid': 0.28; 'print': 0.29; 'cc:addr:python.org': 0.29; '(since': 0.30; 'it.': 0.33; 'header:User-Agent:1': 0.33; 'it?': 0.33; 'file': 0.35; 'encoding': 0.36; 'using': 0.37; 'could': 0.37; 'think': 0.38; 'received:192': 0.38; 'open': 0.38; 'received:192.168.1': 0.39; 'subject:: ': 0.39; 'got': 0.40; 'your': 0.61; 'back': 0.62; 'header:Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.72; 'charset:gb2312': 0.78; 'liked': 0.80; 'subject:write': 0.84; 'subjectcharset:gb2312': 0.99 Date: Thu, 26 Jan 2012 22:01:12 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111109 Thunderbird/3.1.16 MIME-Version: 1.0 To: contro opinion Subject: Re: write =?GB2312?B?1tDOxCBpbnRvIGM6XHQx?= References: In-Reply-To: Content-Type: text/plain; charset=GB2312 Content-Transfer-Encoding: 8bit X-Provags-ID: V02:K0:+AONp9CytpJo9x/mqx4LNxdfEYXV0u7fk24CosBSjG9 kbQq82BjLJgM3cd+uIbGGSrp71g9lgtmKpdLdtoWPfzcPZwZgw OHlv+WRGz3ToJ6ZlReczsyKgiFeiuyKhvMy/ojNCjogitUeInt /m4PzgVdWo6I8X1Jsla4ZIWvZpseSXLmvP0DGTbGGx9ymOp7VV Nl+O4Bk/oU3E9u/XJx+JUNviPCD15EmCy87RQEsMxMT9QHNUQF 6teFoE5VqBYfJthYfsgCvk61vPPt9zt8/JFj36gMph2b5WD0jU h9yNNTyiZK9EFVkZUhgzn0WR7x6fMkqNHyrMvTFg96Cmj91lw= = Cc: python-list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: d@davea.name List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1327633327 news.xs4all.nl 6873 [2001:888:2000:d::a6]:42299 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:19517 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 "", 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