私はPythonには新しく、いくつかのXMLを解析するプログラムを作成しようとしています。私が問題を抱えているところで、.len()
を私がNodeListであると信じているところに呼び出すと、エラー'NodeList' object has no attribute 'len'
が出ます。 documentationが言うので、これは私には本当に驚くべきことである。またPythonでXMLを解析し、 'NodeList'オブジェクトに属性 'len'がありません
を、PythonのDOMインタフェースは、いくつかの追加のサポートがのNodeListオブジェクトはPythonの配列として使用することができるようにするために提供されている必要があります。すべてのNodeListの実装は(をlenをのサポートを含める必要があります)
ここに私のコードです:
import xml.dom.minidom
def testFunction(translationDOM):
textCollection = translationDOM.getElementsByTagName("onscreen_text")
for onscreenText in textCollection:
print textCollection.len()
、その後Main()
...
translationDom = parse(xmlFileName)
testFunction(translationDom)
に私はしたくありませんここに私の全体のXMLを(その大規模な)投稿するが、同様のブロックがいくつかある:
<onscreen_text>
<source id="2036" unique_name="blah" should_be_translated="True">
....
</onscreen_text>
はここで完全なエラー・テキストです:
File "trophytool.py", line 155, in <module>
main()
File "trophytool.py", line 134, in main
testFunction(translationDom)
File "trophytool.py", line 64, in testFunction
print textCollection.len()
AttributeError: 'NodeList' object has no attribute 'len'
あなたはそれが見つかっ<onscreen_text>
タグの数を印刷するだろうと思うだろうが、それはしていません。どうしてこれなの?
「textCollection.length」を試してください – Torious