Enumerating ordered expat attributes with tuplets?

andy_westken at hotmail.com andy_westken at hotmail.com
Thu Sep 11 11:19:22 EDT 2008


On Sep 11, 4:04 pm, Manuel Ebert <maeb... at uos.de> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi Andy,
>
> by the looks of it I'd say that the problem is that the second  
> parameter you passed to start_element is not a dictionary at all (the  
> clue is in the "AttributeError: 'LIST' object" ...).
>
>  >>> d = ['tree', 'house']
>  >>> start_element("Thing", d)
> Thing :
> AttributeError: 'list' object has no attribute 'items'
>  >>> d = {'tree': 'hug', 'flower' : 'eat'}
>  >>> start_element("Thing", d)
> Thing :   flower="eat"  tree="hug"
>
> Manuel
>
> On Sep 11, 2008, at 4:21 PM, andy_west... at hotmail.com wrote:
>
>
>
>
>
> > Hi
>
> > I'm new to Python and trying to pick up good, idiomatic usage right
> > from the offset.
>
> > As I was familiar with Expat from C++ (directly and via expatpp) I'm
> > trying to write a little script - using xml.parsers.expat - to search
> > and replace XML attribute values.
>
> > As I want the attributes to stay in order when the file is written out
> > (so I can check my results with a diff tool) I've set the parser's
> > ordered_attributes attribute. But this has stopped the for loop
> > working with the tuplets.
>
> > The relevant bit of code in my little test, using the default
> > Dictionary for the attributes, is:
>
> > def start_element(name, attrs):
> >     print "%s : " % name,
> >     for (a,b) in attrs.items():
> >         print " %s=\"%s\"" % (a,b),
>
> > But when I set ordered_attributes, first it doesn't like the items()
>
> >     AttributeError: 'list' object has no attribute 'items'
>
> > And then it doesn't like the tuple
>
> >     ValueError: too many values to unpack
>
> > Do I have keep track of where I am (name, value, name, value, ...)
>
> > Or is there a way I can solve the problem with a tuple?
>
> > Thanks, Andy
> > --
> >http://mail.python.org/mailman/listinfo/python-list
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.7 (Darwin)
>
> iD8DBQFIyTOLcZ70OCIgLecRAsBrAJ9YSa7f+YTyM1yRmEKw8KBtb2klIgCgjNzw
> F295Tik+45eNHnJ3B4kKnWU=
> =xR4m
> -----END PGP SIGNATURE------ Hide quoted text -
>
> - Show quoted text -

Sorry!

I forgot to mention that when you set the parser's ordered_attributes
attribute, it sends the "attrs" to start_element as a list, not a
dictionary, in the order name, value, name, value, ...

Andy



More information about the Python-list mailing list