xml到CSV AttributeError:“NoneType”对象没有属性“text”

2024-09-28 19:05:29 发布

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

    for tm in teamtree.iter('team_members'):

我尝试使用上面的函数将这些字段输出到CSV中。 xml数据存储在名为(projectDetJoined)的变量中

我得到了这个错误。在

^{pr2}$

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

你知道为什么找不到它吗?我有一个类似的函数,结构相同,但确实有效。在


Tags: csv数据函数infor错误xml结构
1条回答
网友
1楼 · 发布于 2024-09-28 19:05:29

Ilja Everilä的评论解决了我的问题。在

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.

相关问题 更多 >