Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:not': 0.03; 'indices': 0.07; 'puts': 0.07; 'meeting,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'skip:o 50': 0.09; '""")': 0.16; '"r")': 0.16; 'integers,': 0.16; 'keyerror': 0.16; 'last)': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'skip:j 30': 0.16; 'typeerror:': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'trying': 0.19; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'club': 0.24; 'errors.': 0.24; 'file.': 0.24; 'meeting': 0.26; 'second': 0.26; 'header:X -Complaints-To:1': 0.27; 'subject:list': 0.30; 'doc': 0.31; 'keyerror:': 0.31; 'file': 0.32; 'this.': 0.32; '(most': 0.33; 'third': 0.33; 'skip:d 20': 0.34; 'there': 0.35; 'accessing': 0.36; 'doing': 0.36; 'list': 0.37; 'to:addr:python-list': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'how': 0.40; 'is.': 0.60; 'skip:n 30': 0.60; 'skip:a 30': 0.61; 'first': 0.61; 'you.': 0.62; 'such': 0.63; 'pick': 0.64; 'different': 0.65; 'skip:w 30': 0.69; 'manner': 0.72; 'fourth': 0.84; 'rotary': 0.84; 'seventh': 0.84; 'sixth': 0.84; 'ninth': 0.91; 'race': 0.95 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: xmltodict - TypeError: list indices must be integers, not str Date: Sat, 10 May 2014 14:10:14 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bdbc83.dip0.t-ipconnect.de User-Agent: KNode/4.11.5 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 91 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1399723829 news.xs4all.nl 2922 [2001:888:2000:d::a6]:50669 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:71246 flebber wrote: > I am using xmltodict. > > This is how I have accessed and loaded my file. > > import xmltodict > document = open("/home/sayth/Scripts/va_benefits/20140508GOSF0.xml", "r") > read_doc = document.read() > xml_doc = xmltodict.parse(read_doc) > > The start of the file I am trying to get data out of is. > > date="2014-05-08T00:00:00" gearchanges="-1" stewardsreport="-1" > gearlist="-1" racebook="0" postracestewards="0" meetingtype="TAB" > rail="True" weather="Fine " trackcondition="Dead " > nomsdeadline="2014-05-02T11:00:00" weightsdeadline="2014-05-05T16:00:00" > acceptdeadline="2014-05-06T09:00:00" jockeydeadline="2014-05-06T12:00:00"> > website="http://" /> > stage="Acceptances" distance="1600" minweight="55" raisedweight="0" > class="MDN " age="~ " grade="0" weightcondition="HCP > " trophy="0" owner="0" trainer="0" jockey="0" strapper="0" > totalprize="22000" first="12250" second="4250" third="2100" > fourth="1000" fifth="525" time="2014-05-08T12:30:00" bonustype="BX02 > " nomsfee="0" acceptfee="0" trackcondition=" " timingmethod=" > " fastesttime=" " sectionaltime=" " > formavailable="0" racebookprize="Of $22000. First $12250, second $4250, > third $2100, fourth $1000, fifth $525, sixth $375, seventh $375, eighth > $375, ninth $375, tenth $375"> > > > So thought I had it figured. Can access the elements of meeting and the > elements of club such as by doing this. > > In [5]: xml_doc['meeting']['club']['@abbrevname'] > Out[5]: u'Gosford Race Club' > > However whenever I try and access race in the same manner I get errors. > > In [11]: xml_doc['meeting']['club']['race']['@id'] > --------------------------------------------------------------------------- > KeyError Traceback (most recent call > last) in () > ----> 1 xml_doc['meeting']['club']['race']['@id'] > > KeyError: 'race' > > In [12]: xml_doc['meeting']['race']['@id'] > --------------------------------------------------------------------------- > TypeError Traceback (most recent call > last) in () > ----> 1 xml_doc['meeting']['race']['@id'] > > TypeError: list indices must be integers, not str > > why is accessing race @id any different to the access of club @abbrevname > and how do I get it for race? If I were to guess: there are multiple races per meeting, xmltodict puts them into a list under the "race" key, and you have to pick one: >>> doc = xmltodict.parse("""\ ... ... ... ... ... ... ... """) >>> type(doc["meeting"]["race"]) >>> doc["meeting"]["race"][0]["@id"] 'first race' >>> doc["meeting"]["race"][1]["@id"] 'second race' So xml_doc['meeting']['race'][0]['@id'] or for race in xml_doc["meeting"]["race"]: print(race["@id"]) might work for you.