1

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"))

2 Answers 2

5

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"))
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent, thanks a lot! It worked. I also had to add (*) after [0-9]
1

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"))

2 Comments

Thanks a lot! However, it gives me 'NameError: name 'os' is not defined'. Any advice? Thank you!
whoops, just remove the os from os.listdir

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.