【问题标题】:Converting hundreds of xml file to a single csv file将数百个 xml 文件转换为单个 csv 文件
【发布时间】:2021-08-01 13:10:03
【问题描述】:

我有 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 将它们转换为单个文件?

【问题讨论】:

    标签: python excel xml machine-learning deep-learning


    【解决方案1】:

    遍历每个 XML。在每个 XML 上,使用像 etreelxml 这样的 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 modules 进行解析。

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

    【讨论】:

    • 感谢您的大力帮助。我有点不好意思问这个问题,但我想我必须这样做。我所有的 XML 文件都在一个名为 XML 的文件中。我应该在哪个括号之间使用 XML 定义?因为它给了我“未定义”的错误。我忘了说我想将 XML 转换为一个 CSV 文件以用于机器学习。
    • 在上面添加了新代码。只需将所有 xml 文件复制到一个文件夹中,然后将您的文件夹位置复制粘贴到“myfiles_path”中。它会起作用的。让我知道它是否有效。
    • 谢谢先生的回复。多亏了你,我设法以某种方式写到了 excel,但是这次输出是不规则的。这是我拍的ss。 media.discordapp.net/attachments/376519178372251659/…
    • csv 格式有时会损坏。尝试将其导出到excel。只需将最后一行代码从df.to_csv('output.csv') 替换为df.to_excel('output.xlsx')
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    • 1970-01-01
    • 2020-03-20
    • 2018-11-05
    • 1970-01-01
    相关资源
    最近更新 更多