简体   繁体   中英

Parse multiple XML files to one list of dictionaries in Python

I have a case that when parsing multiple XML files, actually I want the result of the parsing XML to become a single dictionary list instead of multiple dictionary lists.

import glob
from bs4 import BeautifulSoup


def open_xml(filenames):
    for filename in filenames: 
        with open(filename) as fp:
            soup = BeautifulSoup(fp, 'html.parser')
        parse_xml_files(soup)


def parse_xml_files(soup):
    stringToListOfDict = []
    .
    .
    .

    for info in infos:
        dict = {} 
        
        types = info.find_all('type')
        values = info.find_all('value')
        
        for type in types:
            dict[type.attrs['p']] = type.text
      
        stringToListOfDict.append({'Date': Date, 'Time': Time, 'NodeName': node})
        for value in values:
            for result in value.find_all('x'):
                label = dict[result.attrs['y']]
                value = result.text 
                if label:
                    stringToListOfDict[-1][label] = value    

    print(stringToListOfDict)
 
def main():
    open_xml(filenames = glob.glob("*.xml"))

if __name__ == '__main__':
    main() 

With my code above, it always produces two dictionary lists (eg for two XML files) below:

[{'Date': '2020-11-19', 'Time': '18:15', 'NodeName': 'LinuxSuSe','Speed': '16'}]
[{'Date': '2020-11-19', 'Time': '18:30', 'NodeName': 'LinuxRedhat','Speed': '16'}]

The desired output should be one list with two dictionaries only:


[{'Date': '2020-11-19', 'Time': '18:15', 'NodeName': 'LinuxSuSe','Speed': '16'},{'Date': '2020-11-19', 'Time': '18:30', 'NodeName':'LinuxRedhat','Speed': '16'}]

Really appreciated your feedback

print() is used only to send information on screen and it will not join all results in one list.

Your name parse_xml_files is missleading because it parses single file, not all files. And this function should use return to send result for single file and in open_xml you should get this result add to one list - and then you should have all files in one list.

Not tested:

def open_xml(filenames):

    all_files = []

    for filename in filenames: 
        with open(filename) as fp:
            soup = BeautifulSoup(fp, 'html.parser')
        result = parse_xml_file(soup)  # <-- get result from parse_xml_file
        all_files += result  # <-- append result to list 

    print(all_files)  # <-- display all results
    
def parse_xml_file(soup):
    stringToListOfDict = []

    # ... code ...

    for info in infos:
        dict = {} 
        
        types = info.find_all('type')
        values = info.find_all('value')
        
        for type in types:
            dict[type.attrs['p']] = type.text
      
        stringToListOfDict.append({'Date': Date, 'Time': Time, 'NodeName': node})
        for value in values:
            for result in value.find_all('x'):
                label = dict[result.attrs['y']]
                value = result.text 
                if label:
                    stringToListOfDict[-1][label] = value    

    #print(stringToListOfDict)

    return stringToListOfDict  # <-- send to open_xml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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