2017-06-22 99 views
0

假设我有这样的XML文件:获得属性

<article-set xmlns:ns0="http://casfwcewf.xsd" format-version="5"> 
<article> 
<article id="11234"> 
    <source> 
    <hostname>some hostname for 11234</hostname> 
    </source> 
    <feed> 
     <type weight=0.32>RSS</type> 
    </feed> 
    <uri>some uri for 11234</uri> 
</article> 
<article id="63563"> 
    <source> 
    <hostname>some hostname for 63563 </hostname> 
    </source> 
    <feed> 
     <type weight=0.86>RSS</type> 
    </feed> 
    <uri>some uri for 63563</uri> 
    </article> 
. 
. 
. 
</article></article-set> 

我想要的东西,是在RSS其特定的属性权重打印每篇文章的ID为整个文档(如这个)。

id=11234 
weight= 0.32 


id=63563 
weight= 0.86 
. 
. 
. 

我用这个代码,这样做,

from lxml import etree 
tree = etree.parse("C:\\Users\\Me\\Desktop\\public.xml") 


for article in tree.iter('article'): 
    article_id = article.attrib.get('id') 

    for weight in tree.xpath("//article[@id={}]/feed/type/@weight".format(article_id)): 
     print(article_id,weight) 

,并没有工作,可能有人帮助我?

+0

1)请复制粘贴,绝对不要输入您的示例程序,并将数据输入到您的问题中。你的“错别字”是实质性的,并改变了你的问题的性质。 2)请包含一个简短的**完整** XML输入以进行测试。当我更正无效的XML时,测试代码会产生您的预期结果。请参阅[mcve]了解更多信息。 –

+0

另外,“它没有工作”没有帮助。准确地说,预期的结果是什么?究竟什么是实际结果? –

+0

对不起,这是一个诚实的错误。将不会再发生:) –

回答

0

如果你真的想这样做,你可以在两行

>>> from lxml import etree 
>>> tree = etree.parse('public.xml') 
>>> for item in tree.xpath('.//article[@id]//type[@weight]'): 
...  item.xpath('../..')[0].attrib['id'], item.attrib['weight'] 
... 
('11234', '0.32') 
('63563', '0.86') 

一个XML检查我用坚持围绕值双引号weightetree在xml中声明,直到我将第一行放入文件;我不知道为什么。

+0

嘿比尔,感谢您的回复,并且您的代码看起来不错,但它对我不起作用,它卡在循环中,我不知道为什么。它有可能与我的XML文件有关?因为你对另一个问题的帮助在同一个文件上工作得很好 –

+0

请你检查一下你的文件是否可以在'net?它必须是xpath 1.0才能使用Python。 –

0

其中的一个,这可能为你工作:

在这个版本中,请注意在通话中加入=tree.xpath()

from lxml import etree 
tree = etree.parse("news.xml") 


for article in tree.iter('article'): 
    article_id = article.attrib.get('id') 

    for weight in tree.xpath("//article[@id={}]/feed/type/@weight".format(article_id)): 
     print(article_id,weight) 

在这里,请注意,我用article.xpath()取代tree.xpath()

from lxml import etree 
tree = etree.parse("news.xml") 

for article in tree.iter('article'): 
    article_id = article.attrib.get('id') 

    for weight in article.xpath("./feed/type/@weight"): 
     print(article_id,weight) 
+0

第一个,实际上这是一个错字,我忘了把'='放在这里,所以基本上第一个和我在这里一样。但对于第二个我跑它,并没有奏效。我猜在表达式的逻辑中存在一个问题。 (它运行时没有错误) –