Skip to content
Advertisement

Multiple XML files in directory Python

I am fairly new to Python and this community has been a great help! I am learning a lot. I’m trying to use this existing code to loop through multiple XML files in the same directory. Currently, the code is looking at one specific file. Any help is greatly appreciated!

import pandas as pd
from bs4 import BeautifulSoup

soup = BeautifulSoup(open("your_file.xml", "r"), "xml")

d = {}
for tag in soup.RECORDING.find_all(recursive=False):
    d[tag.name] = tag.get_text(strip=True)

df = pd.DataFrame([d])
print(df)

Advertisement

Answer

This should help you…

import pandas as pd
from bs4 import BeautifulSoup
import os

files_in_folder = os.listdir(folder_path)

result = list
for file in files_in_folder:
    soup = BeautifulSoup(open(file, "r"), "xml")
    d = {}
    for tag in soup.RECORDING.find_all(recursive=False):
        d[tag.name] = tag.get_text(strip=True)
        result.append(d)

df = pd.DataFrame(result)
Advertisement