Python 随机非类型对象不可调用错误

Python 随机非类型对象不可调用错误,python,Python,最近,我在应用程序中遇到了以下问题: 文件“main.py”,第31行, 在文件“app.pyc”第205行的运行中 TypeError:“非类型”对象不可调用 我的代码: xml = EXML() for pid, product in store.products.items(): xml.addRow() xml.addCell((product['name']).encode('utf-8'), "String") xml.addCell((product['sy

最近,我在应用程序中遇到了以下问题:

文件“main.py”,第31行, 在文件“app.pyc”第205行的运行中 TypeError:“非类型”对象不可调用

我的代码:

xml = EXML()
for pid, product in store.products.items():
    xml.addRow()
    xml.addCell((product['name']).encode('utf-8'), "String")
    xml.addCell((product['symbol']).encode('utf-8'), "String")
    xml.addCell((product['brand_name']).encode('utf-8'), "String") # line 205
    xml.addCell(str(product['price']), "String")
Python 2.7 32位

它是有线的,因为它在大约1000次迭代后出现,没有任何previus问题。 此应用程序扫描在线商店以获取当前价格。 首先,我认为我遗漏了一些东西,结果是
没有。encode('utf-8')
,但没有。encode('utf-8')似乎可以工作。此外,我无法在测试现场重现此错误,只是在努力使用约2500种产品时有时会出现。 此错误的其他可能来源是什么?

>None.encode
>>> None.encode
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'encode'
>>> None()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
回溯(最近一次呼叫最后一次): 文件“”,第1行,在 AttributeError:“非类型”对象没有属性“encode” >>>无() 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 TypeError:“非类型”对象不可调用

在给定的行上,您必须以某种方式将调用的两个函数之一设置为
None
。您确定这不是下一行,因为覆盖
str
是一个相当常见的错误。

好的,解决了,这有点奇怪,但此错误是由
产品['brand\u name'引起的]
有时是
BeautifulSoup.tag

这次是tag),而不是我计划的
BeautifulSoup.navigablesting
。我仍然不明白为什么和wtf


Anywat,非常感谢您的回复。:)

我同意@Roman。一般来说,我发现避免代码中的长一行是一个很好的做法:您在函数的结果上调用方法,而在方法的结果上调用方法——这更难理解,当您在某个地方遇到错误时,则更难找到。此外,您可以y尝试使用
python-i
运行应用程序,在脚本运行终止后调用解释器,然后
import pdb
并调用
pdb.pm()
。这应该会在您上次回溯时打开Pdb会话,并允许您检查出了什么问题。我确信str没有被覆盖。我想当我在解析器中处理urllib2.HTTPException或AttributeError时,会发生一些不好的情况。我注意到另一个行为:在我的脚本中,我在读取之前关闭了文件句柄,而且只是有时候这引起了错误。