Reputation: 21
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"))
Upvotes: 1
Views: 13649
Reputation: 1290
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"))
Upvotes: 1
Reputation: 6495
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"))
Upvotes: 5