Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #110565
| From | dieter <dieter@handshake.de> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: I seem to be creating a dict that I cannot access the keys of |
| Date | 2016-06-27 09:08 +0200 |
| Message-ID | <mailman.21.1467011309.2358.python-list@python.org> (permalink) |
| References | <8c9b5b13-c6cc-458d-9d31-56f7e5db10c4@googlegroups.com> <871t3llnao.fsf@handshake.de> <mailman.110.1466841055.11516.python-list@python.org> <fcacd6e0-b0d7-45e4-830a-3de93aab4299@googlegroups.com> <8737nz9kin.fsf@handshake.de> |
Sayth Renshaw <flebber.crue@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.
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
I seem to be creating a dict that I cannot access the keys of Sayth Renshaw <flebber.crue@gmail.com> - 2016-06-23 20:59 -0700
Re: I seem to be creating a dict that I cannot access the keys of dieter <dieter@handshake.de> - 2016-06-25 09:50 +0200
Re: I seem to be creating a dict that I cannot access the keys of Sayth Renshaw <flebber.crue@gmail.com> - 2016-06-25 01:41 -0700
Re: I seem to be creating a dict that I cannot access the keys of dieter <dieter@handshake.de> - 2016-06-27 09:08 +0200
csiph-web