0

我有需要应用并存储到变量中的元素列表。由于某种原因它不接受列表中的元素?

import glob
import xmltodict
import lxml.etree as etree

xml_files = glob.glob('dir/*.xml')

list_of_xml = []
for l in [etree.parse(x) for x in xml_files]:
    list_of_xml.append(l)
print(list_of_xml)

# printOutput
[<lxml.etree._ElementTree at 0x17406866b88>,
 <lxml.etree._ElementTree at 0x17406795cc8>,
 <lxml.etree._ElementTree at 0x174068ed7c8>]

for e in list_of_xml():
    store_into_a_variable = xmltodict.parse(etree.tostring(e))


# error: TypeError: 'list' object is not callable

为什么我会收到此错误?我已经多次使用这种循环类型的函数/符号来获得特定的输出。

4

1 回答 1

1

您不会像函数一样调用列表:

for e in list_of_xml():应该for e in list_of_xml:

您的整个片段应该是:

import glob
import xmltodict
import lxml.etree as etree

xml_files = glob.glob('dir/*.xml')

list_of_xml = []
for l in [etree.parse(x) for x in xml_files]:
    list_of_xml.append(l)
print(list_of_xml)

# printOutput
[<lxml.etree._ElementTree at 0x17406866b88>,
 <lxml.etree._ElementTree at 0x17406795cc8>,
 <lxml.etree._ElementTree at 0x174068ed7c8>]

for e in list_of_xml:
    store_into_a_variable = xmltodict.parse(etree.tostring(e))
于 2019-10-02T18:03:00.890 回答