将 XML 文件解析为 Python 对象..?

我尝试这样做,但没有得到如何做到这一点是我的xml.file


<?xml version="1.0" encoding="utf-8"?>

<openerp>

<data>

    <record name ="Collection Report" id="view_collection_report_tree" model="cl.wastemanager.collection.report">

        <field name="description">Collection Report Tree</field>

        <field name="model">ir.ui.view</field>

        <field name="arch" type="xml">

            <tree string="Collection Analysis">

                <field name="hospital_no" type="string"/>

                <field name="partner_id" type="integer"/>

                <field name="district" type="string"/>

                <field name="town" type="string"/>

                <field name="date" type="string"/>

                <field name="cat1_uom_count" type="integer"/>

                <field name="cat1_uom_qty" type="float"/>

                <field name="cat2_uom_count" type="integer"/>

                <field name="cat2_uom_qty" type="float"/>

                <field name="cat3_uom_count" type="integer"/>

                <field name="cat3_uom_qty" type="float"/>

                <field name="cat4_uom_count" type="integer"/>

                <field name="cat4_uom_qty" type="float"/>

                <field name="cat5_uom_count" type="integer"/>

                <field name="cat5_uom_qty" type="float"/>

                <field name="total_uom_count" type="integer"/>

                <field name="total_uom_qty" type="float"/>

                <field name="plant_id" type="integer"/>

                <field name="vehicle_id" type="integer"/>

            </tree>

        </field>

    </record>


</data>

</openerp>

我想把它读成一个python对象,就像字典列表一样。因为标记是绝对固定的,所以我很想使用正则表达式(我非常擅长使用它们)。但是,我想我会检查是否有人知道如何在这里轻松避免正则表达式。不过,我对SAX或其他解析没有太多经验,但我愿意学习。


我期待着看到如何在Python中没有正则表达式的情况下快速完成此操作。感谢您的帮助!


温温酱
浏览 178回答 2
2回答

慕勒3428872

这是我使用熊猫从自己那里尝试的,我能够正确获得输出。import pandas as pdimport xml.etree.ElementTree as etreetree = etree.parse("Filename.xml") #enter your filename what you saved in your systemroot = tree.getroot()columns = ["name", "type"]datatframe = pd.DataFrame(columns = columns)for node in root[0][0][2][0]:&nbsp; &nbsp; name = node.get("name")&nbsp; &nbsp; type = node.get("type")&nbsp; &nbsp; datatframe = datatframe.append(pd.Series([name, type], index = columns), ignore_index = True)print(datatframe)我的输出:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;name&nbsp; &nbsp; &nbsp;type0&nbsp; &nbsp; &nbsp; &nbsp;hospital_no&nbsp; &nbsp;string1&nbsp; &nbsp; &nbsp; &nbsp; partner_id&nbsp; integer2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; district&nbsp; &nbsp;string3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; town&nbsp; &nbsp;string4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; date&nbsp; &nbsp;string5&nbsp; &nbsp; cat1_uom_count&nbsp; integer6&nbsp; &nbsp; &nbsp; cat1_uom_qty&nbsp; &nbsp; float7&nbsp; &nbsp; cat2_uom_count&nbsp; integer8&nbsp; &nbsp; &nbsp; cat2_uom_qty&nbsp; &nbsp; float9&nbsp; &nbsp; cat3_uom_count&nbsp; integer10&nbsp; &nbsp; &nbsp;cat3_uom_qty&nbsp; &nbsp; float11&nbsp; &nbsp;cat4_uom_count&nbsp; integer12&nbsp; &nbsp; &nbsp;cat4_uom_qty&nbsp; &nbsp; float13&nbsp; &nbsp;cat5_uom_count&nbsp; integer14&nbsp; &nbsp; &nbsp;cat5_uom_qty&nbsp; &nbsp; float15&nbsp; total_uom_count&nbsp; integer16&nbsp; &nbsp; total_uom_qty&nbsp; &nbsp; float17&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;plant_id&nbsp; integer18&nbsp; &nbsp; &nbsp; &nbsp;vehicle_id&nbsp; integer

拉丁的传说

您并没有真正说明要实现的目标,但下面是从xml中提取数据的示例,在本例中,我从树元素内的字段中提取字段名称和类型。import xmltodictwith open("test.xml") as xml_file:&nbsp; &nbsp; my_xml = xmltodict.parse(xml_file.read())&nbsp; &nbsp; for field in my_xml["openerp"]["data"]["record"]["field"][2]["tree"]["field"]:&nbsp; &nbsp; &nbsp; &nbsp; print(f"{field['@name']}: {field['@type']}")输出hospital_no: stringpartner_id: integerdistrict: stringtown: stringdate: stringcat1_uom_count: integercat1_uom_qty: floatcat2_uom_count: integercat2_uom_qty: floatcat3_uom_count: integercat3_uom_qty: floatcat4_uom_count: integercat4_uom_qty: floatcat5_uom_count: integercat5_uom_qty: floattotal_uom_count: integertotal_uom_qty: floatplant_id: integervehicle_id: integer
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python