是否有办法知道对象内部是否具有不可迭代的NoneType对象
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了是否有办法知道对象内部是否具有不可迭代的NoneType对象相关的知识,希望对你有一定的参考价值。
因此,我使用vininfo仅使用其VIN从外部DB中填充有关某些单元的信息,并使用此数据,我正在使用dict
或annotate()
将其转换为annotate().items()
,但该脚本分解]
TypeError: cannot unpack non-iterable NoneType object
使用hasattr(obj, 'attr')
尝试,但以相同的错误爆炸。无法使用for..in
进行迭代,因为对象不是可迭代的TypeError: 'NoneType' object is not iterable
。
我在具有相同VIN的CLI版本上试过,它吐出了相同的cannot unpack
错误。
问题是我用来测试的VIN缺少一些准确的数据.transmission
(但我想其他任何字段都会发生),可能是由于填写注册表时的拼写错误,所以我我试图忽略此字段,但我不能,因为它不存在!
VIN是:1NKZXPEX4GJ109010
回溯如下:
Traceback (most recent call last):
File "c:\program files\python37\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "c:\program files\python37\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Program Files\Python37\Scripts\vininfo.exe\__main__.py", line 7, in <module>
File "c:\program files\python37\lib\site-packages\vininfo\cli.py", line 51, in main
entry_point(obj={})
File "c:\program files\python37\lib\site-packages\click\core.py", line 764, in __call__
return self.main(*args, **kwargs)
File "c:\program files\python37\lib\site-packages\click\core.py", line 717, in main
rv = self.invoke(ctx)
File "c:\program files\python37\lib\site-packages\click\core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "c:\program files\python37\lib\site-packages\click\core.py", line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "c:\program files\python37\lib\site-packages\click\core.py", line 555, in invoke
return callback(*args, **kwargs)
File "c:\program files\python37\lib\site-packages\vininfo\cli.py", line 35, in show
out(details)
File "c:\program files\python37\lib\site-packages\vininfo\cli.py", line 24, in out
for k, v in annotatable.annotate().items():
File "c:\program files\python37\lib\site-packages\vininfo\common.py", line 17, in annotate
value = getattr(self, attr_name, no_attr)
File "c:\program files\python37\lib\site-packages\vininfo\details\_base.py", line 53, in __get__
return DetailWrapper(instance, self)
File "c:\program files\python37\lib\site-packages\vininfo\details\_base.py", line 20, in __init__
attr_name, attr_idx = detail.source
TypeError: cannot unpack non-iterable NoneType object
答案
您可以强制传输到空白列表:
>>> vin = vininfo.Vin("1NKZXPEX4GJ109010")
>>> vin.details.transmission = []
>>> vin.details.annotate()
OrderedDict([('Body', 'E'), ('Engine', 'Z'), ('Model', 'X'), ('Plant', 'J'), ('Serial', '109010'), ('Transmission', '')])
以上是关于是否有办法知道对象内部是否具有不可迭代的NoneType对象的主要内容,如果未能解决你的问题,请参考以下文章