简体   繁体   中英

Open and parse multiple XML files from a folder

The Python code below works for one XML. The problem comes when I try to open and parse multiple XML files, which have a similar structure and are saved in the folder (line 4 -> data = open('[0-9].xml',"rb"). I am trying regular expressions, but I am not sure if that works for naming documents too.

The name of all document is "11111.xml, 22222.xml, 33333.xml ..." and so on.

import xml.etree.ElementTree as ET
import re

data = open ('[0-9].xml',"rb")
tree = ET.parse (data)
lst_jugador = tree.findall('data_panel/players/player')
for jugador in lst_jugador:
    print (jugador.find('name').text, jugador.get("id"))

You can use glob module.

import glob
import xml.etree.ElementTree as ET

filenames = glob.glob("[0-9].xml")  # change the pattern to match your case

for filename in filenames:

    with open(filename, 'r', encoding="utf-8") as content:

        tree = ET.parse(content)

        lst_jugador = tree.findall('data_panel/players/player')

        for jugador in lst_jugador:

            print (jugador.find('name').text, jugador.get("id"))

If all your the files in a directory need parsed, you could just use os.listdir()

from os import listdir
for file in listdir(<your directory>):
  #if you have to be more selective inside your directory
  #just add a conditional to skip here
  with open(file, "rb"):
    tree = ET.parse(data)
    lst_jugador = tree.findall('data_panel/players/player')
    for jugador in lst_jugador:
        print (jugador.find('name').text, jugador.get("id"))

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