2016-08-23 39 views
-2
for tm in teamtree.iter('team_members'): 

我试图使用上述函数将这些字段输出到CSV中。 xml数据存储在名为(projectDetJoined)的变量中xml to CSV - AttributeError:'NoneType'对象没有属性'text'

我收到此错误。

Traceback (most recent call last): 
    File "10Other.py", line 481, in <module> 
parseXMLTaskDetails() 
    File "10Other.py", line 355, in parseXMLTaskDetails 
taskcid = (t.find('cid').text) 
AttributeError: 'NoneType' object has no attribute 'text' 

这些项目存在于xml数据中。

任何想法,为什么它没有找到它?我有一个类似的功能,结构相同,但工作。

+1

在你的XML中'team_members'元素没有像'cid'这样的子元素,它有'item'子元素。也许你的意思是'在teamtree.iterfind('team_members/item')'中输入tm。如果您的CSV头文件对于某些项目没有不同的大小写,那么您可以在for循环体中将'tm.findtext'映射到它们上面以提取写入值。不要一直重新打开文件进行追加,而是将XML提取移至最初创建文件和csv-writer的with-block。最后的'csvfile.close()'也是多余的。 –

+0

谢谢。这工作。根据您的建议优化代码。非常感谢。不知道现在到底发生了什么。不知道为什么在这个问题上倒票。但无论如何,这工作得很好,现在我知道了。再次感谢。 –

回答

0

IljaEverilä的评论解决了我的问题。

In your XML the team_members element does not have subelements like cid etc. It has item subelements. Perhaps you meant for tm in teamtree.iterfind('team_members/item'). If your CSV headers didn't have different case for some items, you could've just mapped tm.findtext over them in the for-loop body to extract the values for writing. Don't reopen the file all the time for append, but move the XML extraction to the with-block that initially creates the file and csv-writer. The final csvfile.close() is also redundant.

相关问题