简体   繁体   English

在解析 xml 时没有得到任何类型,即使它有一些价值,我该如何解决这个问题?

[英]while parsing xml getting none type even it has some value,how can i solve this issue?

input输入
As you can see guys the xml code that i am trying to parse,has value in name then how can it be NoneType?如您所见,我正在尝试解析的 xml 代码具有名称价值,那么它怎么可能是 NoneType?

import xml.etree.ElementTree as ET
input='''
  <stuff>
    <user>
      <user x='2' >
        <id>001</id>
        <name>chuck</name>
     </user>
     <user x ='7'>
       <id>009</id>
       <name>brent</name>
       </user>
     </user>
    </stuff>

     '''
stuff=ET.fromstring(input)
print(stuff.find('name').text)

output output

AttributeError             Traceback (most recent call last)
<ipython-input-7-f10e349a116e> in <module>
     16      '''
     17 stuff=ET.fromstring(input)
---> 18 print(stuff.find('name').text)

AttributeError: 'NoneType' object has no attribute 'text'

You need to specify the correct xpath syntax to get to the sub-child name tag.您需要指定正确的xpath 语法以获取子名称标签。

Try this:尝试这个:

print(stuff.find('.//name').text)

Result:结果:

chuck

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 解析 XML 时检查 None 值 - Checking for None value while parsing a XML AttributeError: 'NoneType' object 没有属性 'get' - 当我有数据并且没有字段为 None 时如何解决? - AttributeError: 'NoneType' object has no attribute 'get' - How to solve it when I even have the data and no field is None? 如何解决 Python 代码中的类型错误问题? - How can I solve the Type Error issue in my python code? 以 10 为基数的 int() 的 Django ValueError 无效文字:&#39;&#39;我如何解决 None 类型错误 - Django ValueError invalid literal for int() with base 10: ''how i can solve the None type error 使用Python脚本在XML标签没有值的情况下如何解决StopIteration问题 - How to solve the StopIteration issue while the XML tag does not have a value using Python script 如何从XML int Python中获得一些价值? - How can I get some value from XML int Python? 如何解决用None填充所有值的问题? - How to solve the issue of filling all the values with None? 解析xml文件中的xml标记时如何转义&#39;/&#39;字符 - How '/' character can be escaped while parsing xml tag in an xml file 如何解决“输入形状的预期轴 -1 的值为 1,但收到的输入形状为 [None, 256, 256, 3]&#39;”错误? - How can I solve "expected axis -1 of input shape to have value 1 but received input with shape [None, 256, 256, 3]'" error? 我该如何解决这个 pyinstaller 问题? - how can i solve this pyinstaller issue?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM