【发布时间】:2021-05-06 16:20:09
【问题描述】:
我有一个如下所示的文件:
#This is TEST-data
2020-09-07T00:00:03.230+02:00,ID-10,3,London,Manchester,London,1,1,1
2020-09-07T00:00:03.230+02:00,ID-10,3,London,London,Manchester,1,1
2020-09-07T00:00:03.230+02:00,ID-20,2,London,London,1,1
2020-09-07T00:00:03.230+02:00,ID-20,2,London,London1,1
2020-09-07T00:00:03.230+02:00,ID-30,3,Madrid,Sevila,Sevilla,1,1,1
2020-09-07T00:00:03.230+02:00,ID-30,GGG,Madrid,Sevilla,Madrid,1
2020-09-07T00:00:03.230+02:00,ID-40,GGG,Madrid,Barcelona,1,1,1,1
2020-09-07T00:00:03.230+02:00
2020-09-07T00:00:03.230+02:00
每行中的Index[2] 显示该特定行中有多少城市。所以第一行对于index[2] 的值为3,即London, Manchester, London.
我正在尝试执行以下操作:
对于每一行,我需要检查row [3] + 后面提到的城市(基于城市数量)是否存在于cities_to_filter 中。但这仅在 row[2] 是数字时才需要完成。我还需要解决一些行包含少于 2 个项目的事实。
这是我的代码:
path = r'c:\data\ELK\Desktop\test_data_countries.txt'
cities_to_filter = ['Sevilla', 'Manchester']
def filter_row(row):
if row[2].isdigit():
amount_of_cities = int(row[2]) if len(row) > 2 else True
cities_to_check = row[3:3+amount_of_cities]
condition_1 = any(city in cities_to_check for city in cities_to_filter)
return condition_1
with open (path, 'r') as output_file:
reader = csv.reader(output_file, delimiter = ',')
next(reader)
for row in reader:
if filter_row(row):
print(row)
现在我收到以下错误:
UnboundLocalError: local variable 'condition_1' `referenced before assignment`
【问题讨论】:
-
@mhawke。这就是问题。
-
为什么要在主 for 循环中访问
row[2]?int(row[2])不应该由isdigits()签入filter_row()来保护吗?如果您想打印cities_to_check,请在filter_row()中进行。如果您这样做,您将不会再看到该错误。你会。然而。当引用不存在的amount_of_cities变量时,请参阅从下一行引发的NameError。 -
你打算如何处理那些在
row[2]中包含GGG的行,其中城市计数应该是?您是要忽略这些行,还是仍要尝试检查是否应过滤城市?那些只有时间戳的行呢?默默地忽略它们,将它们转储到 stderr 或中止程序? -
@mhawke 我解决了。谢啦兄弟。感谢您的帮助。
-
我不确定你有没有:)
if row[2].isdigit(): amount_of_cities = int(row[2]) if len(row) > 2 else True搞混了。len(row)保证大于 2,因为if条件已经访问了行中的第三项。此外,将amount_of_cities设置为True会有效地将其设置为1,因为如果将True视为int,则为1。检查我的答案以获取建议的解决方案。
标签: python list if-statement filter