![](/img/trans.png)
[英]Retriving a tag value from multiple XML files in a directory using python
[英]Multiple XML files in directory Python
我對 Python 還很陌生,這個社區提供了很大的幫助。 我學到了很多東西。 我正在嘗試使用此現有代碼循環遍歷同一目錄中的多個 XML 文件,目前。 該代碼正在查看一個特定文件。 任何幫助是極大的贊賞!
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)
您可以為此使用os
模塊的walk
function
import os
path = 'path/to/directory'
for (dirpath, dirnames, filenames) in os.walk(path):
for filename in filenames:
soup = BeautifulSoup(open(os.path.join(dirpath, filename), "r"), "xml")
這應該可以幫助你...
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.