2016-08-23 18 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'サブ要素があります。おそらくあなたはチームツリーのために 'を意味していたでしょう.iterfind( 'team_members/item')'。あなたのCSVヘッダーがいくつかの項目で異なるケースを持っていなかった場合は、forループ本体に 'tm.findtext'をマップして書き込み用の値を抽出できました。追加のために常にファイルを開いてはいけませんが、最初にファイルとcsv-writerを作成したwith-blockにXML抽出を移動してください。最後の 'csvfile.close()'も冗長です。 –

+0

ありがとうございます。それはうまくいった。あなたの提案に従ってコードを最適化しました。どうもありがとう。 iterfindが今まで何だったのか分からなかった。なぜ質問の下に投票するか分からない。しかし、とにかく、これは正常に動作し、今私は知っている。再度、感謝します。 –

答えて

0

イルジャエベラのコメントは私の問題を解決しました。

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.

関連する問題