TypeError:fnmatch.filter()中的“非类型”对象不可iterable

2024-09-28 23:54:10 发布

您现在位置:Python中文网/ 问答频道 /正文

有关以下代码:

print(type(row[0]))
  print(type(df_inputfile[3].tolist()))
  if(row[0] is None):
      print("NONE!")
  else:
      print("NOT NONE")
  if(df_inputfile[3] is None):
      print("NONE!")
  else:
      print("NOT NONE")
  found_labels = fnmatch.filter(df_inputfile[3].tolist(), row[0])
  #print(type(found_labels))

我得到输出:

class 'str'
class 'list'
NOT NONE
NOT NONE
TypeError: 'NoneType' object is not iterable

我不明白为什么会发生这个错误,因为fnmatch.filter的输入是非空的、正确的参数

全部输出为:

debug: 2020-06-09 13:03:13.353673:: FOA_EstimatesAndActuals_Parser.py: Starting application
Processing Source Files\2020 Q1 Post Earnings\ABBV June 2020 Post Earnings D1.xlsx...
Processing Source Files\2020 Q1 Post Earnings\ABBV June 2020 Post Earnings D1.xlsx...
<class 'str'>
<class 'list'>
NOT NONE
NOT NONE
error: 2020-06-09 13:03:18.119950:: FOA_EstimatesAndActuals_Parser.py: Fatal error
Traceback (most recent call last):
  File "C:\Users\moefinger\source\repos\data-engineering\Python\FOA Parser\FOA Parser\FOA_EstimatesAndActuals_Parser.py", line 185, in <module>
    df1, df2 = process_file(regex_file_path, filename, label_file)  # write df_results (both actuals and estimates) and df_results_est (only estimates)
TypeError: 'NoneType' object is not iterable
Press any key to continue . . .

Tags: pynoneparserdfistypenotpost
1条回答
网友
1楼 · 发布于 2024-09-28 23:54:10

请检查列表中是否有非类型元素

我试图为您重现这个问题:

row = ['test1*.py', 'test2*.py', 'test3*.py']
df_inputfile = {
    3: ['test11.py', 'test12.py', 'test13.py', 'test14.py', 'test15.py', None, 'test16.py']
}

print(type(row[0]))
print(type(df_inputfile[3]))

if row[0] is None:
    print("NONE!")
else:
    print("NOT NONE")
if df_inputfile[3] is None:
    print("NONE!")
else:
    print("NOT NONE")
found_labels = fnmatch.filter(df_inputfile[3], row[0])

结果:

<class 'str'>
<class 'list'>
NOT NONE
NOT NONE
Traceback (most recent call last):
  File "C:/Users/Houda/Desktop/DockerPackage/test.py", line 37, in <module>
    test()
  File "C:/Users/Houda/Desktop/DockerPackage/test.py", line 20, in test
    found_labels = fnmatch.filter(df_inputfile[3], row[0])
  File "C:\Program Files\Python37\lib\fnmatch.py", line 60, in filter
    if match(os.path.normcase(name)):
  File "C:\Program Files\Python37\lib\ntpath.py", line 48, in normcase
    s = os.fspath(s)
TypeError: expected str, bytes or os.PathLike object, not NoneType

相关问题 更多 >