I seem to be creating a dict that I cannot access the keys of

dieter dieter at handshake.de
Mon Jun 27 03:08:16 EDT 2016


Sayth Renshaw <flebber.crue at gmail.com> writes:
>> The code below is obviously wrong - it is surprising that you get
>> anything other than an exception. See comments below inserted into
>> your code.
>> 
>> > def parseXML():
>> > ...
>> >             result = etree.tostring(tree.getroot(), pretty_print=True)
>> 
>> "result" here is obviously a string.
>> 
>> >             for sample in result:
>> 
>> This means that "sample" successively hold the characters composing
>> the string "result".
>> 
>> >                 noms = (dict(zip(atts, map(sample.attrib.get, atts))))
>> 
>> You should get "AttributeError: str object does not have attribute `attrib`".
>
> The attrib is an lxml function, when you have defined a root 
> http://lxml.de/tutorial.html

Correctly, this should read: "attrib" is an attribute of "ETree" nodes (!)
(providing a mapping like access to the nodes attributes).

It is *NOT* an attribute of string (!) objects.

You have converted the tree root into a string (using "etree.tostring")
and then iterated over the resulting string. On those characters
you access "attrib" - which will not work as you expect.




More information about the Python-list mailing list