2017-07-03 7 views
0
>>> stringx 
'<?xml version="1.0"?><data><country name="Liechtenstein"><rank>1</rank><year>2008</year><gdppc>141100</gdppc><neighbor name="Austria" direction="E"/><neighbor name="Switzerland" direction="W"/></country><country name="Singapore"><rank>4</rank><year>2011</year><gdppc>59900</gdppc><neighbor name="Malaysia" direction="N"/></country><country name="Panama"><rank>68</rank><year>2011</year><gdppc>13600</gdppc><neighbor name="Costa Rica" direction="W"/><neighbor name="Colombia" direction="E"/></country></data>' 
>>> 
>>> 
>>> e = xml.etree.ElementTree.fromstring(stringx) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
NameError: name 'xml' is not defined 

私はここで何が欠けているのでしょうか?それはXMLコードか、私が解析しようとしている方法ですか?PythonのXML解析中のNameError

+2

郵便からの完全なコード –

答えて

1

import xml.etree.ElementTree

あなたはこれを忘れてしまったように見えます。

適切な使用方法については、ドキュメントを参照してください。

https://docs.python.org/2/library/xml.etree.elementtree.html

編集:ありがとう@mzjn。あなたは正しいです。私はちょうど通り過ぎていて、OPはインポートを忘れていたことに気づいた実際に試してみませんでした。信用あなたに行く:

+0

ちょうど輸入xml'が動作しません '加えます。コードで 'xml.etree.ElementTree.fromstring()'を使うためには、 'import xml.etree.ElementTree'を行う必要があります。 – mzjn

-1

あなたが欠けているようです!

xml.etree輸入ElementTreeの

stringx='<?xml version="1.0"?><data><country name="Liechtenstein"><rank>1</rank><year>2008</year><gdppc>141100</gdppc><neighbor name="Austria" direction="E"/><neighbor name="Switzerland" direction="W"/></country><country name="Singapore"><rank>4</rank><year>2011</year><gdppc>59900</gdppc><neighbor name="Malaysia" direction="N"/></country><country name="Panama"><rank>68</rank><year>2011</year><gdppc>13600</gdppc><neighbor name="Costa Rica" direction="W"/><neighbor name="Colombia" direction="E"/></country></data>' 

e = ElementTree.fromstring(stringx) #will wok fine!