【问题标题】:Issue in extracting value associated with xml tag using python使用python提取与xml标签关联的值的问题
【发布时间】:2017-04-21 04:18:01
【问题描述】:

我有一个python 代码,我在其中解析一个xml 文件并从中提取所有tags。现在我想提取与tag 相关的特定值,但在这样做时发现了一些问题。我的xml 文件示例如下所示:

<Cell ss:StyleID="s65"><Data ss:Type="String">Variable Name</Data></Cell>
    <Cell ss:StyleID="s65"><Data ss:Type="String">Variable Label</Data></Cell>
    <Cell ss:StyleID="s79"><Data ss:Type="String">Minimum&#10;Value</Data></Cell>
    <Cell ss:StyleID="s79"><Data ss:Type="String">Maximum&#10;Value</Data></Cell>
    <Cell ss:StyleID="s80"><Data ss:Type="String">Mean&#10;Value</Data></Cell>

   <Row ss:AutoFitHeight="0" ss:Height="15">
    <Cell ss:StyleID="s73"><Data ss:Type="String">Marks</Data></Cell>
    <Cell ss:StyleID="s73"><Data ss:Type="String">Marks of Students</Data></Cell>
    <Cell ss:StyleID="s82"><Data ss:Type="Number">0</Data></Cell>
    <Cell ss:StyleID="s82"><Data ss:Type="Number">96</Data></Cell>
    <Cell ss:StyleID="s83"><Data ss:Type="Number">65.71</Data></Cell>
   </Row>

现在上面只是我要提取的整个 xml 文件的一部分。我写了这段代码来打印xml文件中的所有标签:

import xml.etree.ElementTree
xmlTree = xml.etree.ElementTree.parse('sample_xml.xml').getroot()

elemList = []

for elem in xmlTree.iter():
  elemList.append(elem.tag) # indent this by tab, not two spaces as I did here

# Just printing out the result

for element in elemList:
    print(element)

现在,当我执行这段代码时,我看到的只是一堆重复的以下示例输出:

{urn:schemas-microsoft-com:office:spreadsheet}Interior
{urn:schemas-microsoft-com:office:spreadsheet}NumberFormat
{urn:schemas-microsoft-com:office:spreadsheet}Protection
{urn:schemas-microsoft-com:office:spreadsheet}Worksheet
{urn:schemas-microsoft-com:office:spreadsheet}Table
{urn:schemas-microsoft-com:office:spreadsheet}Column
{urn:schemas-microsoft-com:office:spreadsheet}Column
{urn:schemas-microsoft-com:office:spreadsheet}Column
{urn:schemas-microsoft-com:office:spreadsheet}Column
{urn:schemas-microsoft-com:office:spreadsheet}Column
{urn:schemas-microsoft-com:office:spreadsheet}Row
{urn:schemas-microsoft-com:office:spreadsheet}Cell
{urn:schemas-microsoft-com:office:spreadsheet}Data
{urn:schemas-microsoft-com:office:spreadsheet}Row
{urn:schemas-microsoft-com:office:spreadsheet}Cell
{urn:schemas-microsoft-com:office:spreadsheet}Data
{urn:schemas-microsoft-com:office:spreadsheet}Row
{urn:schemas-microsoft-com:office:spreadsheet}Cell
{urn:schemas-microsoft-com:office:spreadsheet}Data
{urn:schemas-microsoft-com:office:spreadsheet}Row
{urn:schemas-microsoft-com:office:spreadsheet}Cell
{urn:schemas-microsoft-com:office:spreadsheet}Data
{urn:schemas-microsoft-com:office:spreadsheet}Row
{urn:schemas-microsoft-com:office:spreadsheet}Cell
{urn:schemas-microsoft-com:office:spreadsheet}Data
{urn:schemas-microsoft-com:office:spreadsheet}Row
{urn:schemas-microsoft-com:office:spreadsheet}Cell
{urn:schemas-microsoft-com:office:spreadsheet}Data

我不知道要以哪个单元格、数据、行为目标来提取我需要的值(分数、学生分数、最小值、最大值),如开头示例 xml 格式所示。我该怎么做?

更新:根据建议,我可以使用以下代码提取与代码关联的文本:

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

现在的问题是,在我的 xml 文件中有一堆不同的文本,但我想提取这 4 个标签文本之后的 4 个文本 - Marks,Marks of Students,Minimum Marks,Maximum Marks .当我的当前标签与Marks 匹配时,我尝试使用next() if 迭代器移动到下一个标签,并继续按该顺序匹配下一个3 个标签,但它没有产生所需的结果。这是我写的:

for elem in xmlTree.iter():
    if elem.text == 'Marks':
        if next(xmlTree.iter()) == 'Marks of Students':
            if next(xmlTree.iter()) == 'Minimum Value':
                if next(xmlTree.iter()) == 'Maximum Value':
                    print(next(elem.text))
                    print(next(elem.text))
                    print(next(elem.text))
                    print(next(elem.text))

【问题讨论】:

  • 我无法通过修改您的 XML 以使其格式正确来重现该问题。请发布最小但完整的示例 XML,以及演示问题的相应输出...

标签: python xml parsing xml-parsing


【解决方案1】:

我无法重现您在此处指定的 XML 文件的问题。但我怀疑你的 xml 文件可能是这种格式。

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<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">
<Interior/>
<NumberFormat/>
<Protection/>
<Worksheet ss:Name="Sheet1">
<Table ss:ExpandedColumnCount="6" ss:ExpandedRowCount="2685" x:FullColumns="1"
x:FullRows="1">
<Column ss:AutoFitWidth="0" ss:Width="26.25"/>
<Column ss:AutoFitWidth="0" ss:Width="117" ss:Span="3"/>
<Column ss:Index="6" ss:AutoFitWidth="0" ss:Width="29.25"/>
<Row ss:AutoFitHeight="0" ss:Height="60">
<Cell ss:StyleID="s22"/>
<Cell ss:StyleID="s23"><Data ss:Type="String">Name</Data></Cell>
<Cell ss:StyleID="s23"><Data ss:Type="String">UserName</Data></Cell>
<Cell ss:StyleID="s23"><Data ss:Type="String">Address</Data></Cell>
<Cell ss:StyleID="s23"><Data ss:Type="String">Telephone Number</Data></Cell>
<Cell ss:StyleID="s22"/>
</Row>
<Row ss:AutoFitHeight="0" ss:Height="30">
<Cell ss:StyleID="s22"/>
<Cell ss:StyleID="s24"><Data ss:Type="String">John Smith</Data></Cell>
<Cell ss:StyleID="s24"><Data ss:Type="String">JSmith</Data></Cell>
<Cell ss:StyleID="s24"><Data ss:Type="String">ABC</Data></Cell>
<Cell ss:StyleID="s24"><Data ss:Type="String">(999) 999-9999</Data></Cell>
<Cell ss:StyleID="s22"/>
</Row>
</Table>
</Worksheet>
</Workbook>

如果相同,则可以使用以下代码。

import xml.etree.cElementTree as etree

with open('sample.xml') as xml_file:
    tree = etree.iterparse(xml_file)
    for item in tree:
        if item[1].text != None:
            print item[1].text

我使用下面的参考资料来理解并复制代码。 Reading Excel xml to dictionary

【讨论】:

  • 当我执行for elem in xmlTree.iter(): if elem[1].text != None:print(elem[1].text) 时,我得到IndexError: child index out of range
  • 我能够解决上述错误,但我还有一个问题。在我的 xml 文件中有一堆不同的标签文本。现在我要做的是检查标签文本是否为Marks,然后检查接下来的 3 个标签以查看它们是否为Marks of students,Minimum Marks,Maximum Marks。如果它们被提取,则提取接下来的 4 个标记值,否则继续。我该怎么做?
  • 我已经更新了上面的帖子以反映当前问题
猜你喜欢
  • 2016-06-10
  • 2021-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-25
  • 2015-01-24
相关资源
最近更新 更多