将excelxml读入字典

2024-05-13 05:42:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把简单的excelxml文件读入字典。我试图使用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>

阅读:

^{pr2}$

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

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

Tags: comiddatastringstyletypecellxml
1条回答
网友
1楼 · 发布于 2024-05-13 05:42:03

用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

相关问题 更多 >