0

I have list of elements that I need to apply and store into a variable. For some reason it not accepting elements from list?

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

Why am I getting this error? I have used this for loop kind of function/notation many times to get a certain output.

0

1 Answer 1

1

You don't call a list like a function so:

for e in list_of_xml(): should be for e in list_of_xml:

Your entire snippet should be:

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.