简体   繁体   English

TypeError:iter()不接受关键字参数

[英]TypeError: iter() takes no keyword arguments

I'm working with xml.etree.cElementTree , and regarding to official documentation - want find element in Element : 我正在使用xml.etree.cElementTree ,关于官方文档 -想要在Element查找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? 我在这里想念什么?

This is a known bug; 这是一个已知的错误; the C accelerated version of the API is missing support for the tag parameter as a keyword argument . API的C加速版本缺少对tag参数作为关键字参数的支持 See issue #16849 : 参见问题#16849

Element.{get,iter} doesn't handle keyword arguments when using _elementtree C accelerator. 使用_elementtree C加速器时,Element。{get,iter}不处理关键字参数。

The bug was fixed in Python 3.3 and up but not in Python 2.7 yet. 该错误已在Python 3.3及更高版本中修复,但尚未在Python 2.7中修复。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM