为什么 Python 会在循环中途崩溃?类型错误:__getitem__ [英] Why might Python break down halfway through a loop? TypeError: __getitem__

查看:74
本文介绍了为什么 Python 会在循环中途崩溃?类型错误:__getitem__的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标

我有一个包含 65 个 .txt 文件的目录,我正在逐个解析,并将输出保存到 65 个对应的 .txt 文件中.然后我计划将它们连接起来,但我不确定直接跳到这是否有助于在此处找到解决方案.

I have a directory with 65 .txt files, which I am parsing, one by one, and saving the outputs into 65 corresponding .txt files. I then plan to concatenate them, but I'm not sure if jumping straight to that might help find a solution here.

问题

我收到:

TypeError: 'NoneType' 对象没有属性 'getitem'

TypeError: 'NoneType' object has no attribute 'getitem'

并且看过两个类似的帖子:

and have seen two similar threads:

TypeError: 'NoneType' 对象没有属性 '__getitem__'

Python:TypeError:'NoneType' 对象没有属性 '__getitem__'

然而,我的问题似乎有些奇怪,因为它确实设法浏览输入文件,解析它们并写入输出文件大约十次,此时我得到了错误.这些文件都很相似,只是来自网站的 HTML 源代码(即同一个网站,只是它的不同页面,因此基本的 HTML 结构相同).

My problem seems somewhat strange, however, as it does manage to go through the input files, parsing them and writing the output file about ten times, at which point I get the error. The files are all similar, just HTML source code from website (i.e. the same website, just different pages of it, and so the same basic HTML structure).

这里是出现错误的函数;在此代码段的最后一行:

Here is the function where the error occurs; in the last line of this snippet:

def parse(elTree):
    desired_value = elTree.xpath('my_very_long_xpath')
    desired_value = [x.get('title')[8:] for x in desired_value]

我确实有更多这些变体 - 我实际上正在解析大约 5 到 6 个不同的 desired_value.所有这一切都只是在一个更大的循环中运行,在这个循环中文件被读入 parse 函数,然后输出被写入一个新文件.

I do have a few more variants of these - I am actually parsing for about 5 to 6 different desired_values. And all of this is simply running inside of a larger loop where the files are read in to the parse function and then the output is written to a new file.

我尝试了什么

我已经删除了最初出现错误的文件,但在下一个文件中发生了同样的错误.我又做了同样的事情,删除了两个文件,但仍然出现那个错误.

I have removed the file where I initially got the error, but the same error occurred at the next file. I did the same again, removing two files, but still getting that error.

我在每个文件之间引入了一个 time.sleep(3),只是为了让事情运行得更顺畅.我意识到整个过程可能有一个缓冲区,它可能正在被读取并且只是被擦除,所以那里没有文件......这是一个类似的 C 的循环中出现.不幸的是,3 秒钟的睡眠(加上然后分散在其他各个点)对我没有帮助.代码在完全相同的点失败.

I introduced a time.sleep(3) between each file, just to allow things to maybe run more smoothly. I realized there may be a buffer for the whole process, which is maybe being read and it is just being wiped, and so there is no file there... Here is a similar occurrence within a loop in C. Unfortunately the sleep for 3 seconds (plus then scattered around at various other points) didn't help me. the code fails at exactly the same point.

根据文档,一个TypeError 当一个函数应用于一个不合适类型的对象时会出现,那么它怎么会在正确运行 10 次或 11 次之后发生呢?这里有更多关于 __getitem__信息的官方信息/code> 方法

According to the documentation, a TypeError arises when a function is applied to an object of inappropriate type, so how can it be that it is occurring after functioning correctly 10 or 11 times? Here is more official information regarding the __getitem__ method

由于代码在其他方面运行良好,我没有包括其余的,但如果有人怀疑它可能来自其他地方,有充分的理由,那么我会添加更多代码.

As the code does work well otherwise, I haven't included the rest, but if someone suspects it may originate from somewhere else, with good reason, then I will add more of the code.

我已经检查了 .txt 文件的内容,以查看那些有效的文件和那些失败的文件,并且 xpaths 在这两者中都可以使用,内容可以找到并解析.

I have inspected the contents of the .txt files for those that worked and those where it failed and the xpaths work in both, the contents are there to be found and parsed.

我在同一个文件的 30 个副本上使用了该代码,并且成功执行了,因此 HTML 代码中肯定存在细微的差异,而我的解析器无法识别这些差异.

I used the code on 30 copies of the same file, which did execute successfully, so there must be subtle differences in the HTML code, which my parser is not recognizing.

推荐答案

TypeError: 'NoneType' object has no attribute '__getitem__' 表示您试图使用某种索引,例如 mylist[2],在 None 上,而不是在 list 之类的东西上.这意味着对该对象的 __getitem__ 的内部调用失败,因为 NoneNonetype 类型的对象,没有这样的为它定义的方法.

TypeError: 'NoneType' object has no attribute '__getitem__' means that you attempted to use some kind of indexing, like mylist[2], on None instead of on something like a list. This means that the internal call to that object's __getitem__ failed, because None, which is an object of type Nonetype, doesn't have such a method defined for it.

问题出在 x.get('title')[8:]: get() 方法没有找到任何名为 ' 的键x 中的 title',因此返回 None.但是,您随后尝试使用 [8:] 对其进行切片.如果它返回了一个 list 或类似的对象,它可以正常工作,但对于 None 则不然.

The problem is in x.get('title')[8:]: the get() method didn't find any key called 'title' in x, so it returned None. However, you then try to slice it with [8:]. If it had returned a list or similar object it would work fine, but not so with None.

我建议引入某种错误处理:

I recommend introducing some kind of error handling:

try:
    desired_value = [x.get('title')[8:] for x in desired_value]
except TypeError:
    return

您必须更正和扩展此存根,使其以适合您的程序的方式运行.也许你需要定义某种默认的 desired_value 或其他东西,而不是 return 语句.

You will have to correct and expand this stub to make it behave in a way that's appropriate for your program. Maybe instead of a return statement you'll need to define some kind of default desired_value or something.

这篇关于为什么 Python 会在循环中途崩溃?类型错误:__getitem__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆