When parsing a single xml file to find a specific named node and it's text, I can get output as desired. However, I need to extend this code to a collection of xml files. When I do so, I get no output.
Here is my code on a single xml file, which works as desired:
from lxml import etree
parsed_single = etree.parse('Downloads/one_file.xml')
xp_eval =etree.XPathEvaluator(parsed_single)
d = dict((item.text, item.tag) for item in xp_eval('//node_of_interest'))
The above code outputs the following, as expected:
d=
{'interesting text': 'Node_of_interest',
'more inter text': 'Node_of_interest',
'yet more txt': 'Node_of_interest'}
However, when applying the above code file by file to a folder of very similar xml files, my dictionary is empty. Here is the code attempt at parsing a collection of xml files:
import glob
import os
path = '/Users/Downloads/XML_FOLDER/'
read_files = glob.glob(os.path.join(path, '*.xml'))
for file in read_files:
file_pars = etree.parse(file)
xpatheval = etree.XPathEvaluator(file_pars)
full_dict = dict((item.text, item.tag) for item in xpatheval('//node_of_interest'))
Unfortunately, full_dict = {}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…