Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeder.news-service.com!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'sys': 0.04; 'wed,': 0.04; 'parser': 0.05; '__name__': 0.07; 'ascii': 0.07; 'character,': 0.07; 'exception,': 0.07; 'specifying': 0.07; 'unexpected': 0.07; '"__main__":': 0.09; '%s"': 0.09; 'subject:parsing': 0.09; 'pm,': 0.11; 'def': 0.13; 'wrote:': 0.14; '"unexpected': 0.16; 'ascii,': 0.16; 'confused.': 0.16; 'exception?': 0.16; 'main():': 0.16; 'non-standard': 0.16; 'subject:XML': 0.16; 'wonderful.': 0.16; '\xa0for': 0.16; '\xa0print': 0.16; 'thanks,': 0.17; '27,': 0.19; 'header:In-Reply-To:1': 0.22; 'column': 0.22; 'file,': 0.22; 'parse': 0.23; 'subject:problem': 0.25; 'script': 0.26; 'xml': 0.26; "i'm": 0.26; 'skip:[ 10': 0.26; 'url:mailman': 0.27; 'tried': 0.27; 'message-id:@mail.gmail.com': 0.28; "doesn't": 0.28; 'raise': 0.29; 'error': 0.29; 'offending': 0.31; 'stops': 0.31; 'second': 0.31; "can't": 0.31; "skip:' 10": 0.32; 'import': 0.32; 'to:addr:python-list': 0.32; 'url:listinfo': 0.33; 'test': 0.33; 'record': 0.34; 'using': 0.34; 'change': 0.34; 'there': 0.35; 'file': 0.35; 'characters': 0.35; 'url:python': 0.37; 'apr': 0.38; 'received:google.com': 0.38; 'but': 0.38; 'url:org': 0.38; 'anything': 0.38; 'docs': 0.39; 'mike': 0.39; 'to:addr:python.org': 0.39; "it's": 0.40; '2011': 0.62; 'perfectly': 0.65; 'strange': 0.65; 'cause': 0.65; '(id': 0.84; 'encoding?': 0.84; 'expects': 0.84; 'received:129': 0.84; 'records:': 0.84 MIME-Version: 1.0 In-Reply-To: References: Date: Wed, 27 Apr 2011 14:41:49 -0400 Subject: Re: ElementTree XML parsing problem From: Benjamin Kaplan To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Junkmail-Status: score=10/49, host=mpv2.tis.cwru.edu X-Junkmail-Signature-Raw: score=unknown, refid=str=0001.0A02020A.4DB8636F.0175,ss=1,fgs=0, ip=74.125.82.182, so=2010-12-23 16:51:53, dmn=2009-09-10 00:05:08, mode=single engine X-Junkmail-IWF: false X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list 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: 80 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1303929721 news.xs4all.nl 81483 [::ffff:82.94.164.166]:58853 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:4148 On Wed, Apr 27, 2011 at 2:26 PM, Mike wrote: > I'm using ElementTree to parse an XML file, but it stops at the second > record (id =3D 002), which contains a non-standard ascii character, =E4. = Here's > the XML: > > > > > > > > > > > The complaint offered up by the parser is > > Unexpected error opening simple_fail.xml: not well-formed (invalid token)= : > line 5, column 40 > > and if I change the line to eliminate the =E4, everything is wonderful. T= he > parser is perfectly happy with this modification: > > > > I can't find anything in the ElementTree docs about allowing additional t= ext > characters or coercing strange ascii to Unicode. > > Is there a way to coerce the text so it doesn't cause the parser to raise= an > exception? > Have you tried specifying the file encoding? =E4 is not "strange ascii". It's outside the ASCII range so if the parser expects ASCII, it will get confused. > Here's my test script (simple_fail contains the offending line, and > simple_pass contains the line that passes). > > import sys > import xml.etree.ElementTree as ET > > def main(): > > =A0 =A0xml_files =3D ['simple_fail.xml', 'simple_pass.xml'] > =A0 =A0for xml_file in xml_files: > > =A0 =A0 =A0 =A0print > =A0 =A0 =A0 =A0print 'XML file: %s' % (xml_file) > > =A0 =A0 =A0 =A0try: > =A0 =A0 =A0 =A0 =A0 =A0tree =3D ET.parse(xml_file) > =A0 =A0 =A0 =A0except Exception, inst: > =A0 =A0 =A0 =A0 =A0 =A0print "Unexpected error opening %s: %s" % (xml_fil= e, inst) > =A0 =A0 =A0 =A0 =A0 =A0continue > > =A0 =A0 =A0 =A0root =3D tree.getroot() > =A0 =A0 =A0 =A0records =3D root.find('records') > =A0 =A0 =A0 =A0for record in records: > =A0 =A0 =A0 =A0 =A0 =A0print record.attrib['id'], record.attrib['educatio= n'] > > if __name__ =3D=3D "__main__": > =A0 =A0 =A0 =A0main() > > > Thanks, > > -- Mike -- > > -- > http://mail.python.org/mailman/listinfo/python-list >