属性错误 'Nonetype'对象没有属性。但我检查无类型



我得到AttributeError: 'NoneType' object has no attribute 'rstrip'

所以我添加了if clist is not None:,但我一直得到相同的错误。为什么?

if clist is not None:
result = [
[name, clist.rstrip()] 
for name, clist in zip(
fragments[::group_count+1],
fragments[group_count::group_count+1]
)
]

完整回溯

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [85], in <module>
120             fragments = fragments[1:]
121         if clist is not None:
--> 122             result = [
123                 [name, clist.rstrip()] 
124                 for name, clist in zip(
125                     fragments[::group_count+1],
126                     fragments[group_count::group_count+1]
127                 )
128             ]
Input In [85], in <listcomp>(.0)
120             fragments = fragments[1:]
121         if clist is not None:
122             result = [
--> 123                 [name, clist.rstrip()] 
124                 for name, clist in zip(
125                     fragments[::group_count+1],
126                     fragments[group_count::group_count+1]
127                 )
128             ]
AttributeError: 'NoneType' object has no attribute 'rstrip'

试试这个

list out of list comprehension不是None而是None里面的一些值片段,这就是为什么你会得到这个错误。

您需要添加if clist is not None inside list comprehension如下。

result = [[name, clist.rstrip()] for name, clist in zip(
fragments[::group_count+1],
fragments[group_count::group_count+1]
) if clist is not None]

最新更新