这是我的档案:
#This is TEST-data
2020-09-07T00:00:03.230+02:00,ID-10,3,Lon,Man,Lon,1,1,1
2020-09-07T00:00:03.230+02:00,ID-10,3,Lon,Lon,Man,1,1
2020-09-07T00:00:03.230+02:00,ID-20,2,Lon,Lon,1,1
2020-09-07T00:00:03.230+02:00,ID-20,2,Lon,Lon,1
2020-09-07T00:00:03.230+02:00,ID-30,3,Mad,Sev,Sev,1,1,1
2020-09-07T00:00:03.230+02:00,ID-30,GGG,Mad,Sev,Mad1
2020-09-07T00:00:03.230+02:00,ID-40,GGG,Mad,Bar,1,1,1,1
2020-09-07T00:00:03.230+02:00
2020-09-07T00:00:03.230+02:00当我在代码下面运行时,我会得到一个空的返回。这可能是因为我的代码似乎不知道Man在曼彻斯特,Sev在塞维利亚。我认为condition_1中出现了这个问题
path = r'c:\data\ELK\Desktop\test_data_countries.txt'
cities_to_filter = ['Sevilla', 'Manchester']
def filter_row(row):
if len(row) > 2 and row[2].isdigit():
amount_of_cities = int(row[2])
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)这是我的预期输出:
2020-09-07T00:00:03.230+02:00,ID-10,3,Lon,Man,Lon,1,1,1
2020-09-07T00:00:03.230+02:00,ID-10,3,Lon,Lon,Man,1,1
2020-09-07T00:00:03.230+02:00,ID-30,3,Mad,Sev,Sev,1,1,1发布于 2021-02-02 16:38:37
您可以用.split()函数拆分每一行,这个函数将字符串拆分为您给定的参数,如果不提供参数,它将分隔空格字符串。然后它将返回一个列表,因此您应该将它分配给一个列表。然后控制列表中的"man“或列表中的"sev”。
for line in file:
myList=line.split(",")
if "man" in myList or "sev" in myList:
#blabla发布于 2021-02-02 16:41:50
问题是,您正在尝试将字符串Man与Manchester匹配。
您可以使用以下仅与前三个字符匹配:
import csv
path = 'Pdata.txt'
cities_to_filter = ['Sevilla', 'Manchester']
def filter_row(row):
if len(row) > 2 and row[2].isdigit():
amount_of_cities = int(row[2])
cities_to_check = row[3:3+amount_of_cities]
#print(cities_to_check)
condition_1 = any(city[:3] 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)要匹配任何字符,而不仅仅是前三个字符,可以使用两个列表来查找任何匹配的行。
因此,如果将cities_to_check作为["Man", "vil"],则匹配将包含两个['Sevilla', 'Manchester'],然后可以使用len(matching) != 0作为返回条件来获得所需的结果。
cities_to_filter = ['Sevilla', 'Manchester']
cities_to_check = ["Man", "vil"]
matching = [city2 for city2 in cities_to_filter if any(city1 in city2 for city1 in cities_to_check)]https://stackoverflow.com/questions/66013793
复制相似问题