Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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 xml解析莫名其妙地终止了_Python_Xml_Beautifulsoup - Fatal编程技术网

Python xml解析莫名其妙地终止了

Python xml解析莫名其妙地终止了,python,xml,beautifulsoup,Python,Xml,Beautifulsoup,我有一个用格式良好的XML包装的句子填充的文件(xmllint和tidylib这样说)。 因此,xml如下所示: <a id="100" attr1="text" attr1="text" attr1="text"> <tagname id="1"> This is my sentence. </tagname> </a> <a id="101" attr1="text" attr1="text" attr1="text"> <t

我有一个用格式良好的XML包装的句子填充的文件(xmllint和tidylib这样说)。 因此,xml如下所示:

<a id="100" attr1="text" attr1="text" attr1="text">
<tagname id="1">
This is my sentence.
</tagname>
</a>
<a id="101" attr1="text" attr1="text" attr1="text">
<tagname id="1">
This is my sentence.
</tagname>
</a>
所有内容都打印得很好,直到第84句,我收到了错误: achild=a.find('标记名') AttributeError:“非类型”对象没有属性“查找”

每套。。。是使用for循环生成的,因此xml完全相同。 我试过使用不同的文件和不同数量的句子。发生错误的id也会更改。 这是beautifulsoup的限制吗?
它不能扫描超过一定数量的行吗?

它在最后一行失败。这可能是一个文件编码问题,该行包含一些有趣的EOF字符,或者该行没有被解释为字符串。你能在最后一行失败之前打印出来,看看它是什么类型的吗?

很可能
a=soup.find('a',{'id':i})
with
84
不会返回你期望的结果<如果找不到标记,则code>find()返回
None
,从而解释
AttributeError

此外,在代码中,您似乎正在优化列表(表示为字符串)

你在列一个清单,然后再把清单扩大,这很愚蠢。如果有一个
id
,那么如何处理整个文件,然后循环遍历每个a标记

from bs4 import BeautifulSoup
with open('file.xml', 'r') as myfile:
    soup = BeautifulSoup(myfile.read())
    for i in soup.find_all('a', id=True):
        print i.tagname.contents
印刷品:

[u'\nThis is my sentence.\n']
[u'\nThis is my sentence.\n']

id号84是什么样子的?soup=beautifulsou(myfile.read())使我的python空闲GUI崩溃。该文件包含大约140000个文件sentences@waterling那可能不是最好的选择。
from bs4 import BeautifulSoup
with open('file.xml', 'r') as myfile:
    soup = BeautifulSoup(myfile.read())
    for i in soup.find_all('a', id=True):
        print i.tagname.contents
[u'\nThis is my sentence.\n']
[u'\nThis is my sentence.\n']