簡體   English   中英

將Excel xml讀取到字典

[英]Reading Excel xml to dictionary

我想將簡單的excel xml文件閱讀為字典。 我嘗試使用xlrd 7.1但它返回格式錯誤。 現在,我正在嘗試使用xml.etree.ElementTree ,也沒有成功。 我無法更改.xml文件的結構。 這是我的代碼:

<?xml version="1.0" encoding="UTF-8"?>
-<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:html="http://www.w3.org/TR/REC-html40">
  -<Styles>
    -<Style ss:Name="Normal" ss:ID="Default">
      <Alignment ss:Vertical="Bottom"/>
      <Borders/>
      <Font ss:FontName="Verdana"/>
      <Interior/>
      <NumberFormat/>
      <Protection/>
    </Style> -<Style ss:ID="s22">
      <NumberFormat ss:Format="General Date"/>
    </Style>
  </Styles> -<Worksheet ss:Name="Linkfeed">
    -<Table>
      -<Row>
        -<Cell>
          <Data ss:Type="String">ID</Data>
        </Cell> -<Cell>
          <Data ss:Type="String">URL</Data>
        </Cell>
      </Row> -<Row>
        -<Cell>
          <Data ss:Type="String">22222</Data>
        </Cell> -<Cell>
          <Data ss:Type="String">Hello there</Data>
        </Cell>
      </Row>
    </Table>
  </Worksheet>
</Workbook>

讀:

import xml.etree.cElementTree as etree

def xml_to_list(fname):
        with open(fname) as xml_file:
                tree = etree.parse(xml_file)

                for items in tree.getiterator(tag="Table"):
                        for item in items: # Items is None!
                                print item.text

更新,現在可以使用,但是如何排除垃圾?

def xml_to_list(fname):
        with open(fname) as xml_file:
                tree = etree.iterparse(xml_file)
                for item in tree:
                        print item[1].text

用if語句排除“垃圾”:

def xml_to_list(fname):
    with open(fname) as xml_file:
            tree = etree.iterparse(xml_file)
            for item in tree:
                 if item[1].text.strip() != '-':
                        print item[1].text

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM