Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x XML分析错误AttributeError:';非类型';对象没有属性';文本';_Python 3.x_Xml_Xml Parsing - Fatal编程技术网

Python 3.x XML分析错误AttributeError:';非类型';对象没有属性';文本';

Python 3.x XML分析错误AttributeError:';非类型';对象没有属性';文本';,python-3.x,xml,xml-parsing,Python 3.x,Xml,Xml Parsing,我的问题可能有一个简单的解决方案,但我对python3非常陌生,所以请对我放松;) 我有一个简单的脚本正在运行,它已经使用此代码成功地解析了xml文件中的信息 import xml.etree.ElementTree as ET root = ET.fromstring(my_xml_file) u = root.find(".//name").text.rstrip() print("Name: %s\n" % u) 我正在解析的xml如下所示 <?xml version="1.0

我的问题可能有一个简单的解决方案,但我对python3非常陌生,所以请对我放松;)

我有一个简单的脚本正在运行,它已经使用此代码成功地解析了xml文件中的信息

import xml.etree.ElementTree as ET

root = ET.fromstring(my_xml_file)

u = root.find(".//name").text.rstrip()
print("Name: %s\n" % u)
我正在解析的xml如下所示

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/3.2/style/exchange.xsl"?>
<example:world-data xmlns="http://www.example.org" xmlns:ops="http://example.oorg" xmlns:xlink="http://www.w3.oorg/1999/xlink">
  <exchange-documents>
    <exchange-document system="acb.org" family-id="543672" country="US" doc-number="95962" kind="B2">
      <bibliographic-data>
             <name>SomeName</name>
       ...and so on... and ends like this
   </exchange-document>
  </exchange-documents>
</example:world-data>
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/3.2/style/pub-ftxt-claims.xsl"?>
<ops:world-data xmlns="http://www.example.org/exchange" xmlns:example="http://example.org" xmlns:xlink="http://www.example.org/1999/xlink">
  <ftxt:fulltext-documents xmlns="http://www.examp.org/fulltext" xmlns:ftxt="ww.example/fulltext">
    <ftxt:fulltext-document system="example.org" fulltext-format="text-only">
      <bibliographic-data>
        <publication-reference data-format="docdb">
          <document-id>
            <country>EP</country>
            <doc-number>10000</doc-number>
            <kind>A</kind>
          </document-id>
        </publication-reference>
      </bibliographic-data>
      <claims lang="EN">
        <claim>
          <claim-text>1. Some text.</claim-text>
          <claim-text>2. Some text.</claim-text>
          <claim-text>2. Some text.</claim-text>
        </claim>
      </claims>
    </ftxt:fulltext-document>
  </ftxt:fulltext-documents>
</ops:world-data>
但是,如果我尝试使用相同的python命令从相同的api解析另一个xml,则会得到以下错误代码

AttributeError: 'NoneType' object has no attribute 'text'
第二个xml文件如下所示

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/3.2/style/exchange.xsl"?>
<example:world-data xmlns="http://www.example.org" xmlns:ops="http://example.oorg" xmlns:xlink="http://www.w3.oorg/1999/xlink">
  <exchange-documents>
    <exchange-document system="acb.org" family-id="543672" country="US" doc-number="95962" kind="B2">
      <bibliographic-data>
             <name>SomeName</name>
       ...and so on... and ends like this
   </exchange-document>
  </exchange-documents>
</example:world-data>
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/3.2/style/pub-ftxt-claims.xsl"?>
<ops:world-data xmlns="http://www.example.org/exchange" xmlns:example="http://example.org" xmlns:xlink="http://www.example.org/1999/xlink">
  <ftxt:fulltext-documents xmlns="http://www.examp.org/fulltext" xmlns:ftxt="ww.example/fulltext">
    <ftxt:fulltext-document system="example.org" fulltext-format="text-only">
      <bibliographic-data>
        <publication-reference data-format="docdb">
          <document-id>
            <country>EP</country>
            <doc-number>10000</doc-number>
            <kind>A</kind>
          </document-id>
        </publication-reference>
      </bibliographic-data>
      <claims lang="EN">
        <claim>
          <claim-text>1. Some text.</claim-text>
          <claim-text>2. Some text.</claim-text>
          <claim-text>2. Some text.</claim-text>
        </claim>
      </claims>
    </ftxt:fulltext-document>
  </ftxt:fulltext-documents>
</ops:world-data>
预期产量

1. Some text.
但它只打印上述错误消息。 为什么我可以使用这些命令解析第一个xml而不是第二个xml

非常感谢您的帮助

编辑:Jack Fleeting的代码在python控制台中工作,但不幸的是在我的PyCharm中不工作

from lxml import etree
root = etree.XML(my_xml.encode('ascii'))
root2 = etree.XML(my_xml2.encode('ascii'))

root.xpath('//*[local-name()="name"]/text()')
root2.xpath('//*[local-name()="claim-text"]/text()')
这可能是我的魅力中的一只虫子吗?我第一次提到的代码片段仍然为name打印正确的结果

编辑:结果我不得不使用

a = root3.xpath('//*[local-name()="claim-text"]/text()')
print(a, flush=True)

在我们找到可能的解决方案之前,这里有几个问题。第一,您提供的第一个xml片段无效(例如,
未关闭)。我意识到这只是一个片段,但因为这是我们必须处理的,所以我修改了下面的片段来解决这个问题。第二,两个代码段都有带有未绑定(未使用)前缀的xmlns声明(第一个是
example:world data
,第二个是
ops:world data
)。我也必须去掉这些前缀,其余的才能工作

考虑到这些修改,使用lxml库应该适合您

第一个修改的代码段:

 my_xml = """<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/3.2/style/exchange.xsl"?>
<world-data xmlns="http://www.example.org" xmlns:ops="http://example.oorg" xmlns:xlink="http://www.w3.oorg/1999/xlink">
  <exchange-documents>
    <exchange-document system="acb.org" family-id="543672" country="US" doc-number="95962" kind="B2">
      <bibliographic-data>
             <name>SomeName</name>
       ...and so on... and ends like this
         </bibliographic-data>
   </exchange-document>
  </exchange-documents>
</world-data>"""
输出:

['SomeName']

输出:

['1.一些文字','2.一些文字','3.一些文字']


在这两种情况下,您的预期输出是什么?我刚刚添加了所需的信息。谢谢,您的解决方案似乎在我的python控制台中工作。但是,如果我尝试在PyCharm中运行您的代码,我不会得到任何结果,只是“进程已完成,退出代码为0”@qwyxkl-对不起,我对PyCharm不太熟悉。这里面一定有什么东西,因为我检查了这段代码,它确实有效(例如在Jupyter中)。我的问题解决了,因为您的解决方案在技术上有效。谢谢你的帮助;)不过,我不能在任何地方执行您的代码,只能在python shell中执行。我试着从Pycharm到Atom再到其他在线编译器,甚至换了电脑,但仍然没有luck@qwyxkl-你知道吗-如果从中提取xml的完整url不是机密的,你能把它(或完整代码)发布到某个地方吗?这是一个与你上面问的不同的问题,但现在我真的很想知道问题是什么……我找到了一个打印它的解决方案:我必须通过flush参数强制输出,现在它工作了。但是谢谢你的努力
my_xml2 = """<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/3.2/style/pub-ftxt-claims.xsl"?>
<world-data xmlns="http://www.example.org/exchange" xmlns:example="http://example.org" xmlns:xlink="http://www.example.org/1999/xlink">
  <ftxt:fulltext-documents xmlns="http://www.examp.org/fulltext" xmlns:ftxt="ww.example/fulltext">
    <ftxt:fulltext-document system="example.org" fulltext-format="text-only">
      <bibliographic-data>
        <publication-reference data-format="docdb">
          <document-id>
            <country>EP</country>
            <doc-number>10000</doc-number>
            <kind>A</kind>
          </document-id>
        </publication-reference>
      </bibliographic-data>
      <claims lang="EN">
        <claim>
          <claim-text>1. Some text.</claim-text>
          <claim-text>2. Some text.</claim-text>
          <claim-text>3. Some text.</claim-text>
        </claim>
      </claims>
    </ftxt:fulltext-document>
  </ftxt:fulltext-documents>
</world-data>"""
from lxml import etree
root = etree.XML(my_xml.encode('ascii'))
root2 = etree.XML(my_xml2.encode('ascii'))

root.xpath('//*[local-name()="name"]/text()')
root2.xpath('//*[local-name()="claim-text"]/text()')