无法解析XML并将数据导入pandas数据帧

时间:2017-09-24 06:01:23

标签: python xml pandas dataframe

我正在尝试从包含运动测试的逐个呼吸数据的XML文件中导入数据。 XML结构如下(简化显示一般结构):

<?xml version="1.0"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
   xmlns:o="urn:schemas-microsoft-com:office:office"
   xmlns:x="urn:schemas-microsoft-com:office:excel"
   xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
   xmlns:html="http://www.w3.org/TR/REC-html40">
    <Worksheet ss:Name="MetasoftStudio">
        <Table ss:ExpandedColumnCount="21" ss:ExpandedRowCount="458" x:FullColumns="1"    x:FullRows="1" ss:StyleID="s62" ss:DefaultColumnWidth="53">
            <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="137"/>
            <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="97"/>
            <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="137"/>
            <Row ss:AutoFitHeight="0" ss:Height="26">
                <Cell ss:StyleID="Default"><Data ss:Type="String">t</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="String">Phase</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="String">Marker</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="String">V'O2</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="String">V'O2/kg</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="String">V'O2/HR</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="String">HR</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="String">WR</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="String">V'E/V'O2</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="String">V'E/V'CO2</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="String">RER</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="String">V'E</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="String">BF</Data></Cell>
            </Row>
            <Row ss:Height="15">
                <Cell ss:StyleID="Default"><Data ss:Type="String">h:mm:ss</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="String"></Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="String"></Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="String">L/min</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="String">ml/min/kg</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="String">ml</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="String">/min</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="String">W</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="String"></Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="String"></Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="String"></Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="String">L/min</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="String">/min</Data></Cell>
            </Row>
            <Row ss:Height="15">
                <Cell ss:StyleID="Default"><Data ss:Type="String">0:00:06</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="String">Rest</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="String"></Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="Number">0.27972413565454501</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="Number">4.3706896196022598</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="Number">4.5856415681072953</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="Number">61</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="Number">0</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="Number">27.002532271037801</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="Number">26.4113108545688</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="Number">1.0223851598932201</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="Number">10.155340000000001</Data></Cell>
                <Cell ss:StyleID="Default"><Data ss:Type="Number">18.07</Data></Cell>
            </Row>
        </Table>
    </Worksheet>
</Workbook>

我使用lxml来解析和迭代XML文件,然后提取每个“单元格”中的“数据”,将其附加到列表中,然后将该列表附加到父列表(给我一个嵌套每行的列表)使用代码:

from lxml import etree, objectify
import pandas as pd

with open('Python/cortex.xml') as infile:
    xml_file = infile.read()

    root = objectify.fromstring(xml_file)

    header = []
    data = []

    for row in root.Worksheet.Table.getchildren():
        temp_row = []
        if not row.tag == '{urn:schemas-microsoft-com:office:spreadsheet}Column':
            for cell in row.getchildren():
                temp_row.append(cell.Data)
            data.append(temp_row)
    header = data.pop(0) #remove the first 'row' and store in header list
    del data[0] #remove 2nd line of superfluous data

第一行给出了标题,因此我pop进入它自己的列表,第2行包含每个变量的单位,所以我只是摆脱它。到目前为止一切运作良好(或似乎如此)...

现在我需要将其转换为pd数据框以开始使用它。如果我去df = pd.DataFrame(data, columns=header)而我print(df)我得到: ValueError: Buffer has wrong number of dimensions (expected 1, got 32)

好吧不确定那里发生了什么......如果我在没有指定标题和打印的情况下制作df:

              0           1       2                        3   \
0  [[[0:00:06]]]  [[[Rest]]]  [[[]]]  [[[0.279724135654545]]]   
1  [[[0:00:09]]]  [[[Rest]]]  [[[]]]  [[[0.465136232899829]]]   
2  [[[0:00:13]]]  [[[Rest]]]  [[[]]]  [[[0.357975433456662]]]   
3  [[[0:00:19]]]  [[[Rest]]]  [[[]]]  [[[0.543332419057909]]]   
4  [[[0:00:24]]]  [[[Rest]]]  [[[]]]  [[[0.374604578743889]]]

看起来不对劲!列表中列表中的所有这些列表都来自哪里!如果我迭代并打印嵌套列表data,它会完美打印,但是一旦我尝试将其转换为df,就会出错。

任何人都可以告诉我发生了什么以及如何将数据输入pd df?如果有一种比我做得更好的方法,那么我很乐意试一试。

1 个答案:

答案 0 :(得分:0)

您可以按构造函数创建列表列表,然后创建DataFrame。对于解析使用this solution

from lxml import etree

with (open('test.xml','r')) as f:
    doc = etree.parse(f)

namespaces={'o':'urn:schemas-microsoft-com:office:office',
            'x':'urn:schemas-microsoft-com:office:excel',
            'ss':'urn:schemas-microsoft-com:office:spreadsheet'}

L = []
ws = doc.xpath('/ss:Workbook/ss:Worksheet', namespaces=namespaces)
if len(ws) > 0: 
    tables = ws[0].xpath('./ss:Table', namespaces=namespaces)
    if len(tables) > 0: 
        rows = tables[0].xpath('./ss:Row', namespaces=namespaces)
        for row in rows:
            tmp = []
            cells = row.xpath('./ss:Cell/ss:Data', namespaces=namespaces)
            for cell in cells:
#                print(cell.text);
                tmp.append(cell.text)
            L.append(tmp)
print (L)
[['t', 'Phase', 'Marker', "V'O2", "V'O2/kg", "V'O2/HR", 'HR', 'WR', 
  "V'E/V'O2", "V'E/V'CO2", 'RER', "V'E", 'BF'], 
 ['h:mm:ss', None, None, 'L/min', 'ml/min/kg', 'ml', 
 '/min', 'W', None, None, None, 'L/min', '/min'], 
 ['0:00:06', 'Rest', None, '0.27972413565454501', '4.3706896196022598',
  '4.5856415681072953', '61', '0', '27.002532271037801', '26.4113108545688', 
  '1.0223851598932201', '10.155340000000001', '18.07']]

df = pd.DataFrame(L[2:], columns=L[0])
print (df)
         t Phase Marker                 V'O2             V'O2/kg  \
0  0:00:06  Rest   None  0.27972413565454501  4.3706896196022598   

              V'O2/HR  HR WR            V'E/V'O2         V'E/V'CO2  \
0  4.5856415681072953  61  0  27.002532271037801  26.4113108545688   

                  RER                 V'E     BF  
0  1.0223851598932201  10.155340000000001  18.07