Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.6k views
in Technique[技术] by (71.8m points)

python - Code using lxml and xpath works on single xml file, but fails when this is extended to a collection of similar xml

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 = {}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...