python xml 解析期间出现 NameError
NameError during python xml parsing
>>> 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 代码还是我尝试解析的方式?
import xml.etree.ElementTree
看来你忘记了这个。
参考文档以正确使用。
https://docs.python.org/2/library/xml.etree.elementtree.html
编辑:谢谢@mzjn。你是对的。我只是路过,注意到 OP 忘记了导入。没有实际尝试过。归功于你:)
您似乎缺少导入!
from xml.etree import 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!
python 3
from xml.etree import 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)
print(e) # <Element 'data' at 0x7f7e7a4cc1d8>
>>> 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 代码还是我尝试解析的方式?
import xml.etree.ElementTree
看来你忘记了这个。
参考文档以正确使用。
https://docs.python.org/2/library/xml.etree.elementtree.html
编辑:谢谢@mzjn。你是对的。我只是路过,注意到 OP 忘记了导入。没有实际尝试过。归功于你:)
您似乎缺少导入!
from xml.etree import 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!
python 3
from xml.etree import 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)
print(e) # <Element 'data' at 0x7f7e7a4cc1d8>