[英]Reading Excel xml to dictionary
I want to read simple excel xml file to dictionary. 我想将简单的excel xml文件阅读为字典。 I have tried to use xlrd 7.1
but it returns format errors. 我尝试使用xlrd 7.1
但它返回格式错误。 Now i'm trying to use xml.etree.ElementTree
and also without success. 现在,我正在尝试使用xml.etree.ElementTree
,也没有成功。 I can't change structure of .xml file. 我无法更改.xml文件的结构。 Here my code: 这是我的代码:
<?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>
Reading: 读:
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
Update, now it works, but how to exclude junk? 更新,现在可以使用,但是如何排除垃圾?
def xml_to_list(fname):
with open(fname) as xml_file:
tree = etree.iterparse(xml_file)
for item in tree:
print item[1].text
Exclude "junk" with an if-statement: 用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.