Enumerating ordered expat attributes with tuplets?

andy_westken at hotmail.com andy_westken at hotmail.com
Thu Sep 11 10:21:43 EDT 2008


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



More information about the Python-list mailing list