Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed5.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'output': 0.04; 'encoded': 0.05; 'utf-8': 0.07; 'codecs': 0.09; 'encode': 0.09; 'english,': 0.09; 'encoding': 0.15; "'r',": 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'gbk,': 0.16; 'input:': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'subject:unicode': 0.16; 'wrote:': 0.17; 'specify': 0.17; 'test.': 0.17; 'unicode': 0.17; '(in': 0.18; '>>>': 0.18; 'skip:" 30': 0.20; 'import': 0.21; 'parse': 0.22; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'skip:" 20': 0.26; 'received:192.168.1.3': 0.29; "skip:' 10": 0.30; 'expect': 0.31; 'file': 0.32; 'received:84': 0.32; 'material': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'form.': 0.33; "can't": 0.34; 'text': 0.34; 'something': 0.35; 'test': 0.36; 'two': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'skip:" 10': 0.40; 'received:192.168': 0.40; 'skip:u 10': 0.60; 'save': 0.61; 'is.': 0.62; 'header:Reply-To:1': 0.68; 'reply-to:no real name:2**0': 0.72; 'reply-to:addr:python.org': 0.84; 'skip:: 40': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=IekFqBWa c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=lR6CHUT36vYA:10 a=_jf2IAAgYN0A:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=g5FfwMEU4gAA:10 a=A92VS8N7Nf97cxhW7l0A:9 a=QEXdDO2ut3YA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Fri, 07 Sep 2012 02:14:06 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/20120824 Thunderbird/15.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: lxml can't output right unicode result References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org 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: 66 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1346980441 news.xs4all.nl 6849 [2001:888:2000:d::a6]:52970 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:28656 On 07/09/2012 01:21, contro opinion wrote: > i eidt a file and save it in gbk encode named test. my system is > :debian,locale,en.utf-8;python2.6,locale,utf-8. > > >

> > > in terminal i input: > > xxd test > > 0000000: 3c68 746d 6c3e 0a3c 703e c4e3 3c2f 703e .

..

> 0000010: 0a3c 2f68 746d 6c3e 0a .. > > 你 is you in english, > "\xc4\xe3" is the gbk encode of it. > "\xe4\xbd\xe3" is the utf-8 encode of it. > "u\x4f\x60" is the unicode encode of it. > now i parse it in lxml > > >>> "你" > '\xe4\xbd\xa0' > >>> "你".decode("utf-8") > u'\u4f60' > >>> "你".decode("utf-8").encode("gbk") > '\xc4\xe3' > >>> > > code1: > > >>> import lxml.html > >>> root=lxml.html.parse("test") > >>> d=root.xpath("//p") > >>> d[0].text_content() > u'\xc4\xe3' > > in material ,lxml parse file to output the unicode form. > why the d[0].text_content() can not output u'\x4f\x60'? > > code2: > > import codecs > import lxml.html > f = codecs.open('test', 'r', 'gbk') > root=lxml.html.parse(f) > d=root.xpath("//p") > d[0].text_content() > u'\xe4\xbd\xa0' > > why the d[0].text_content() can not output u'\x4f\x60'? > > i am confused by this problem for two days. > You can't just put some text into a file and expect it to know "magically" what the encoding is. You have to specify that the encoding is GBK, something like this (in a file actually encoded as GBK, of course):

I hope there's a good reason why you're using that encoding and not UTF-8.