使用python从XML .xls文件中提取特定的单元格数据

时间:2018-01-15 19:55:08

标签: python xml excel python-2.7

我有一个巨大的XML文件,它从设备导出为.xls文件。

            <?xml version='1.0'?>
        <?mso-application progid='Excel.Sheet'?>
        <s:Workbook xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:s="urn:schemas-microsoft-com:office:spreadsheet">
          <s:Styles>
            ...
          <s:Worksheet s:Name="Description">
       ...
    <s:Worksheet s:Name="Data">
        <s:Table s:DefaultColumnWidth="100">
          <s:Row>
            <s:Cell s:StyleID="Bold">
              <s:Data s:Type="String">Time</s:Data>
            </s:Cell>
            <s:Cell s:StyleID="Bold">
              <s:Data s:Type="String">Temp1</s:Data>
            </s:Cell>
            <s:Cell s:StyleID="Bold">
              <s:Data s:Type="String">Temp2</s:Data>
            </s:Cell>
            <s:Cell s:StyleID="Bold">
              <s:Data s:Type="String">Liquid</s:Data>
            </s:Cell>
            <s:Cell s:StyleID="Bold">
              <s:Data s:Type="String">Response</s:Data>
            </s:Cell>
            <s:Cell s:StyleID="Bold">
              <s:Data s:Type="String">Base</s:Data>
            </s:Cell>
            <s:Cell s:StyleID="Bold">
              <s:Data s:Type="String">Events</s:Data>
            </s:Cell>
            <s:Cell s:StyleID="Bold">
              <s:Data s:Type="String">Low</s:Data>
            </s:Cell>
            <s:Cell s:StyleID="Bold">
              <s:Data s:Type="String">High</s:Data>
            </s:Cell>
            <s:Cell />
          </s:Row>
       ...
      <s:Row>
        <s:Cell s:StyleID="Default">
          <s:Data s:Type="Number">45</s:Data> # Time
        </s:Cell>
          # There is no Temp1 data
        <s:Cell />
        <s:Cell s:StyleID="Default">
          <s:Data s:Type="Number">29.74</s:Data> # Temp2
        </s:Cell>
        <s:Cell s:StyleID="Default">
          <s:Data s:Type="Number">12.11</s:Data> # Liquid
        </s:Cell>
        <s:Cell s:StyleID="Default">
          <s:Data s:Type="Number">100</s:Data> # Response
        </s:Cell>
        <s:Cell s:StyleID="Default">
          <s:Data s:Type="Number">30</s:Data> # Base
        </s:Cell>
          # There are no events in this data
        <s:Cell />
        <s:Cell s:StyleID="Default">
          <s:Data s:Type="Number">0</s:Data> # Low
        </s:Cell>
        <s:Cell s:StyleID="Default">
          <s:Data s:Type="Number">55</s:Data> # High
        </s:Cell>
        <s:Cell />
      </s:Row>

我要做的是从名为&#34; Data。&#34;的工作表中提取信息。数据有9个标题,但我只对与&#34; Time&#34;相对应的数据感兴趣。和&#34; Temp2&#34;,这将是&#34; 45&#34;和&#34; 29.74&#34;分别。

我已经设法弄清楚如何使用以下方式导航文件:

import xml.etree.ElementTree as ET

tree = ET.parse('xmlfile')
root = tree.getroot()

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

root.findall('./s:Worksheet/s:Table/s:Row/s:Cell/s:Data', namespaces=ns)

我从单元格中获取数据的最接近的是使用我在另一篇文章中找到的示例,并尝试以下变体:

for elem in xmlTree.iter():
    if elem.text != None:
        print(elem.text)

这会输出所有(所有18901行数据),我真的不知道如何从这里开始。最终我想要做的是将这些数据存储在数据框架或类似的东西中,以便我可以绘制它。

1 个答案:

答案 0 :(得分:0)

这可能是一个天真的建议,但您是否尝试过简单地使用Pandas(当然,在安装软件包之后)?

import pandas
df = pandas.read_excel(excel_file)

# ... analyze and plot from the DataFrame

(这可能是评论,但我还不准评论......)

相关问题