python - 将数百个 xml 文件转换为单个 csv 文件
问题描述
我有 423 个 xml 文件来训练我的深度学习模型。我搜索了一些 python 代码和 xslt 等,但我不知道该怎么做。以下是一个文件的示例:
<?xml version="1.0"?>
-<case>
<number>2</number>
<age>49</age>
<sex>F</sex>
<composition>solid</composition>
<echogenicity>hyperechogenicity</echogenicity>
<margins>well defined</margins>
<calcifications>non</calcifications>
<tirads>2</tirads>
<reportbacaf/>
<reporteco/>
-<mark>
<image>1</image>
<svg>[{"points": [{"x": 250, "y": 72}, {"x": 226, "y": 82}, {"x": 216, "y": 90}, {"x": 204, "y": 94}, {"x": 190, "y": 98}, {"x": 181, "y": 103}, {"x": 172, "y": 109}, {"x": 165, "y": 121}, {"x": 161, "y": 131}, {"x": 159, "y": 142}, {"x": 162, "y": 170}, {"x": 164, "y": 185}, {"x": 171, "y": 203}, {"x": 176, "y": 210}, {"x": 185, "y": 214}, {"x": 191, "y": 218}, {"x": 211, "y": 228}, {"x": 212, "y": 230}, {"x": 235, "y": 239}, {"x": 243, "y": 242}, {"x": 255, "y": 244}, {"x": 263, "y": 245}, {"x": 263, "y": 245}, {"x": 285, "y": 244}, {"x": 298, "y": 242}, {"x": 330, "y": 233}, {"x": 352, "y": 217}, {"x": 367, "y": 201}, {"x": 373, "y": 194}, {"x": 379, "y": 173}, {"x": 382, "y": 163}, {"x": 383, "y": 143}, {"x": 383, "y": 136}, {"x": 382, "y": 127}, {"x": 379, "y": 122}, {"x": 374, "y": 117}, {"x": 365, "y": 109}, {"x": 360, "y": 101}, {"x": 358, "y": 95}, {"x": 352, "y": 88}, {"x": 346, "y": 85}, {"x": 333, "y": 81}, {"x": 327, "y": 78}, {"x": 319, "y": 73}, {"x": 314, "y": 72}, {"x": 304, "y": 70}, {"x": 281, "y": 69}, {"x": 258, "y": 71}, {"x": 254, "y": 71}, {"x": 248, "y": 72}], "annotation": {}, "regionType": "freehand"}]</svg>
</mark>
</case>
我需要解析(包括)数字和 tirads 之间的信息。如何使用 Python 将这些转换为单个文件?
解决方案
循环遍历每个 XML。在每个 XML 上,使用etree或lxml之类的 xml 解析器来解析 xml 的内容并将其存储为 dict。然后将字典保存为 JSON 文件或 CSV。
您要解析的标签列表,
tags = ['number','age','sex','composition','echogenicity', 'margins', 'calcifications', 'tirads']
解析保存为“abc.xml”的XML,
with open('abc.xml', 'r') as fd:
doc = fd.read()
使用Lxml & BeautifulSoup 模块进行解析。
from bs4 import BeautifulSoup as BX
soup = BX(doc, 'lxml')
mydata = {}
for tag in tags:
value = soup.find(tag)
if value:
mydata[tag] = value.text
else:
mydata[tag] = None
检查数据
print(mydata)
#{'number': '2', 'age': '49', 'sex': 'F', 'composition': 'solid', 'echogenicity': 'hyperechogenicity', 'margins': 'well defined', 'calcifications': 'non', 'tirads': '2'}
完整的代码写成一个函数,
from bs4 import BeautifulSoup as BX
tags = ['number','age','sex','composition','echogenicity', 'margins', 'calcifications', 'tirads']
def parse_xml(xmlfile):
with open(xmlfile, 'r') as fd:
doc = fd.read()
soup = BX(doc, 'lxml')
mydata = {}
for tag in tags:
value = soup.find(tag)
if value:
mydata[tag] = value.text
else:
mydata[tag] = None
return mydata
您可以循环所有 xml 文件并使用此函数解析每个 xml。
#lets say all your xml files are in this folder
myfiles_path = r"C:/Users/RG/Desktop/test/"
import os, pandas
all_data = {}
xmlfiles = os.listdir(myfiles_path)
for file in xmlfiles:
file_path = os.path.join(myfiles_path, file)
all_data[file] = parse_xml(file_path)
df = pandas.DataFrame.from_dict(all_data)
df.to_csv('output.csv')
推荐阅读
- android - 我用什么来代替 android.support.v7.preference.Preference 中删除的 onCreateView() 方法?
- javascript - 如何在 python (flask) 中读取 multiDicts
- vb.net - 我是否需要关闭或类似的声明才能将第二个文件(在同一运行会话中)上传到网站?
- jackson - Jackson:如何将日期序列化为 dd/MMM/yy
- android - Android MVVM:视图是否应该为每个用户交互(甚至是微不足道的交互)通知视图模型(只有 UI 结果/没有数据)
- prediction - 在将 CV 与 groupKFold() 一起使用以保留数据集中的重要分组后,如何在测试集上进行预测
- uwp - 无法使用 UdpClient 多播到“239.255.255.250”
- regex - 删除具有特定编号字符的每一行 - Notepad++
- ios - 在对 loadFileRepresentation 的多次调用完成后运行一个块
- google-apps-script - 在一个电子表格上使用 onEdit 触发器在另一个电子表格上设置值