python - XML 写入按元素分组的关系表

标签 python xml list hierarchical

我在将此 XML 中的数据(示例和很多类似)重新排列为数组时遇到问题。

<?xml version="1.0" encoding="UTF-8"?> <bon>
    <bonnr>10010100001361599999</bonnr>
    <datum>2020-01-03T13:19:18.244+01:00</datum>
    <dynamicFieldsItem>
        <key>COUNTER</key>
        <value>5779</value>
    </dynamicFieldsItem>
    <dynamicFieldsItem>
        <key>CSNAME</key>
        <value>User Break Over 170001</value>
    </dynamicFieldsItem>
    <rskabu>
        <bulfnr>1</bulfnr>
        <buverk></buverk>
        <bupost>1</bupost>
        <buart>BRU</buart>
        <buartn>14112500</buartn>
        <buwgrp></buwgrp>
        <bumwstkz>1</bumwstkz>
        <bumwsts>7.700</bumwsts>
        <bumwstb>0.21</bumwstb>
        <dynamicFieldsItem>
            <key>ARTIKEL-LABEL</key>
            <value>grün 15</value>
        </dynamicFieldsItem>
        <dynamicFieldsItem>
            <key>CAPTURE</key>
            <value>SCAN</value>
        </dynamicFieldsItem>
        <dynamicFieldsItem>
            <key>MWSTART</key>
            <value>A</value>
        </dynamicFieldsItem>
        <dynamicFieldsItem>
            <key>MWSTKZ</key>
            <value>79</value>
        </dynamicFieldsItem>
        <dynamicFieldsItem>
            <key>W2GRP</key>
            <value>113</value>
        </dynamicFieldsItem>
    </rskabu>
    <rskabu>
        <bulfnr>2</bulfnr>
        <buverk></buverk>
        <bupost>2</bupost>
        <buart>BRU</buart>
        <buartn>14112599</buartn>
        <buwgrp></buwgrp>
        <bumwstkz>1</bumwstkz>
        <bumwsts>7.700</bumwsts>
        <bumwstb>0.21</bumwstb>
        <dynamicFieldsItem>
            <key>ARTIKEL-LABEL</key>
            <value>Glock</value>
        </dynamicFieldsItem>
        <dynamicFieldsItem>
            <key>CAPTURE</key>
            <value>SCAN</value>
        </dynamicFieldsItem>
        <dynamicFieldsItem>
            <key>MWSTART</key>
            <value>A</value>
        </dynamicFieldsItem>
    </rskabu>
    <rskab4>
        <bupost>1</bupost>
        <dynamicFieldsItem>
            <key>ZCC</key>
            <value>30.00</value>
        </dynamicFieldsItem>
        <dynamicFieldsItem>
            <key>CC</key>
            <value>OFF</value>
        </dynamicFieldsItem>
    </rskab4>
    <rskdwp>
        <dwlfnr>1</dwlfnr>
        <dwrow>0</dwrow>
        <dwcol>0</dwcol>
        <dwcpi>-1</dwcpi>
        <dwcmd>28</dwcmd>
        <dwtext>CUE</dwtext>
    </rskdwp>
    <rskdwp>
        <dwlfnr>2</dwlfnr>
        <dwrow>0</dwrow>
        <dwcol>0</dwcol>
        <dwcpi>-1</dwcpi>
        <dwcmd>28</dwcmd>
        <dwtext>END</dwtext>
    </rskdwp> </bon>

我认为我将数据写入不同的表,如下面列表中所示。 然后将数据插入数据库就很容易了。

BON
---
bonnr,datum
10010100001361599999,2020-01-03T13:19:18.244+01:00

BON_DYNAMIC
-----------
bonnr,datum,key,value
10010100001361599999,2020-01-03T13:19:18.244+01:00,COUNTER,5779
10010100001361599999,2020-01-03T13:19:18.244+01:00,CSNAME,User Break Over 170001

BON_RSKABU
----------
bonnr,datum,bulfnr,buverk,bupost,buart,buartn,buwgrp,bumwstkz,bumwsts,bumwstb
10010100001361599999,2020-01-03T13:19:18.244+01:00,1,,1,BRU,14112500,,,1,7.700,0.21
10010100001361599999,2020-01-03T13:19:18.244+01:00,2,,2,BRU,14112599,,,1,7.700,0.21

BON_RSKABU_DYN
--------------
bonnr,datum,bulfnr,key,value
10010100001361599999,2020-01-03T13:19:18.244+01:00,1,ARTIKEL-LABEL,grün 15
10010100001361599999,2020-01-03T13:19:18.244+01:00,1,CAPTURE,SCAN
10010100001361599999,2020-01-03T13:19:18.244+01:00,1,MWSTART,A
10010100001361599999,2020-01-03T13:19:18.244+01:00,1,MWSTKZ,79
10010100001361599999,2020-01-03T13:19:18.244+01:00,1,W2GRP,113
10010100001361599999,2020-01-03T13:19:18.244+01:00,2,ARTIKEL-LABEL,Glock
10010100001361599999,2020-01-03T13:19:18.244+01:00,2,CAPTURE,SCAN
10010100001361599999,2020-01-03T13:19:18.244+01:00,2,MWSTART,A

BON_RSKAB4
----------
bonnr,datum,bupost,key,value
10010100001361599999,2020-01-03T13:19:18.244+01:00,1,ZCC,30.00
10010100001361599999,2020-01-03T13:19:18.244+01:00,1,CC.OFF

BON_RSKDWP
----------
bonnr,datum,dwlfnr,dwrow,dwcol,dwcpi,dwcmd,dwtext
10010100001361599999,2020-01-03T13:19:18.244+01:00,1,0,0,-1,28,CUE
10010100001361599999,2020-01-03T13:19:18.244+01:00,1,0,0,-1,28,END

问题主要在于来自不同分支的分组属性。 首先,我花了一些时间尝试解释数据,这对于非常简单的情况有用 一种 XML。 如果有人可以向我展示一个 Python 示例,如何针对 BON_RSKABU 或 BON_RSKABU_DYN 这样的示例执行此操作,我会非常高兴。问题是从不同层次结构级别选择数据并填充数据,即使它只出现一次。

最佳答案

在 python 中,您可以使用 lxml、xpath 和 pandas 来完成此操作。当我们进行时我会解释。我们将以您的 BON_RSKABU 为例:

import lxml.html
import pandas as pd

bonn = """[your html above]""" #you may need to delete your top encoding delclaration (<?xml version="1.0" encoding="UTF-8"?> )
root = etree.fromstring(bonn) #parse the html

columns = [] #initialize lists for collecting the headers and the table rows
rows = []
bonn = root.xpath('.//bonnr')#locate header 1
dats = root.xpath('.//datum')#locate header 2
rsks = root.xpath('//rskabu') #locate the data itself
columns.append(bonn[0].tag) #add the 1st and 2nd column items to the columns list
columns.append(dats[0].tag)
for head in rsks[0][:9]: #you are only interested in the first 9 items, so append their names to the column list as well
    columns.append(head.tag)

for rsk in rsks: #now collect the data for each row
    row=[]
    row.append(bonn[0].text) #add the first 2 standalone data items to your row
    row.append(dats[0].text)
    item = rsk.xpath('.//*') #locate the rest of the data
    for i in item[:9]:  #now add the rest of the data (but only the first 9) to your row
        row.append(i.text)        
    rows.append(row) #add this row to the final rows list

df = pd.DataFrame(rows,columns=columns) #now create a pandas dataframe to house all this
df

输出(抱歉格式问题):

bonnr datum bulfnr buverk bupost buart buartn buwgrp bumwstkz bumwsts bumwstb 10010100001361599999 2020-01-03T13:19:18.244+01:00 1 None 1 BRU 14112500 None 7.700 0.21 10010100001361599999 2020-01-03T13:19:18.244+01:00 2 None 2 BRU 14112599 None 7.700 0.21

关于python - XML 写入按元素分组的关系表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59922348/

相关文章:

java - 通过套接字传输 List<> 对象时出现的问题

python - 试验 python 线程,如何停止线程

python - 重定向在 apache beam 中意味着什么(python)

c# xml 序列化分组子节点中的元素

performance - sorted 和 sortBy 的区别

Java 推断类型和列表 : Working through general Quicksort example

python - 如何解析与选项混合的多个位置参数?

python - 如何使 wx.TextEntryDialog 变大和可调整大小

python - 编程错误 : column "xxxxx" of relation "xxxx" does not exist

xml - 我可以在 Delphi 中将 msxml.IXMLDOMNode 转换为 XmlIntf.IXMLNode 吗?