Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #4148

Re: ElementTree XML parsing problem

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 <bsk16@case.edu>
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 <ip9n72$ol6$1@dont-email.me>
References <ip9n72$ol6$1@dont-email.me>
Date Wed, 27 Apr 2011 14:41:49 -0400
Subject Re: ElementTree XML parsing problem
From Benjamin Kaplan <benjamin.kaplan@case.edu>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.900.1303929721.9059.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Wed, Apr 27, 2011 at 2:26 PM, Mike <Mike@invalid.invalid> wrote:
> I'm using ElementTree to parse an XML file, but it stops at the second
> record (id = 002), which contains a non-standard ascii character, ä. Here's
> the XML:
>
> <?xml version="1.0"?>
> <snapshot time="Mon Apr 25 08:47:23 PDT 2011">
> <records>
> <record id="001" education="High School" employment="7 yrs" />
> <record id="002" education="Universität Bremen" employment="3 years" />
> <record id="003" education="River College" employment="5 yrs" />
> </records>
> </snapshot>
>
> 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 ä, everything is wonderful. The
> parser is perfectly happy with this modification:
>
> <record id="002" education="University Bremen" employment="3 yrs" />
>
> I can't find anything in the ElementTree docs about allowing additional text
> 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? ä 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():
>
>    xml_files = ['simple_fail.xml', 'simple_pass.xml']
>    for xml_file in xml_files:
>
>        print
>        print 'XML file: %s' % (xml_file)
>
>        try:
>            tree = ET.parse(xml_file)
>        except Exception, inst:
>            print "Unexpected error opening %s: %s" % (xml_file, inst)
>            continue
>
>        root = tree.getroot()
>        records = root.find('records')
>        for record in records:
>            print record.attrib['id'], record.attrib['education']
>
> if __name__ == "__main__":
>        main()
>
>
> Thanks,
>
> -- Mike --
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

ElementTree XML parsing problem Mike <Mike@invalid.invalid> - 2011-04-27 11:26 -0700
  Re: ElementTree XML parsing problem Benjamin Kaplan <benjamin.kaplan@case.edu> - 2011-04-27 14:41 -0400
  Re: ElementTree XML parsing problem Neil Cerutti <neilc@norwich.edu> - 2011-04-27 19:24 +0000
    Re: ElementTree XML parsing problem Mike <Mike@invalid.invalid> - 2011-04-27 13:43 -0700
  Re: ElementTree XML parsing problem Philip Semanchuk <philip@semanchuk.com> - 2011-04-27 15:32 -0400
  Re: ElementTree XML parsing problem Hegedüs Ervin <airween@gmail.com> - 2011-04-27 21:33 +0200
    Re: ElementTree XML parsing problem Mike <Mike@invalid.invalid> - 2011-04-27 13:32 -0700
  Re: ElementTree XML parsing problem Stefan Behnel <stefan_ml@behnel.de> - 2011-04-28 07:57 +0200
  Re: ElementTree XML parsing problem Ervin Hegedüs <airween@gmail.com> - 2011-04-28 08:24 +0200

csiph-web