setevoy
setevoy

Reputation: 4662

TypeError: iter() takes no keyword arguments

I'm working with xml.etree.cElementTree, and regarding to official documentation - want find element in Element:

$ python --version
Python 2.7.8

My script:

#!/usr/bin/env python

import os, re

import xml.etree.ElementTree as ET

XML_FILE = '/cygdrive/****.csproj'

try:
    tree = ET.ElementTree(file=XML_FILE)
    root = tree.getroot()

    print type(root)

    for item in root.iter(tag='OutputPath'):
        print item.tag, item.attrib, item.text
    ....

But when I run it - have an error:

$ ./xm_par.py
<type 'Element'>
Traceback (most recent call last):
  File "./xm_par.py", line 21, in <module>
    for item in root.iter(tag='OutputPath'):
TypeError: iter() takes no keyword arguments

What I'm miss here?

Upvotes: 2

Views: 2102

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121844

This is a known bug; the C accelerated version of the API is missing support for the tag parameter as a keyword argument. See issue #16849:

Element.{get,iter} doesn't handle keyword arguments when using _elementtree C accelerator.

The bug was fixed in Python 3.3 and up but not in Python 2.7 yet.

You can omit the keyword and pass in the argument as a positional instead:

for item in root.iter('OutputPath'):

Demo:

>>> import xml.etree.cElementTree as ET
>>> tree = ET.fromstring('''\
... <root>
...     <OutputPath></OutputPath>
... </root>
... ''')
>>> tree.iter(tag='OutputPath')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: iter() takes no keyword arguments
>>> tree.iter('OutputPath')
<generator object iter at 0x1045cc5a0>

Upvotes: 3

Related Questions