mardi 2 février 2021

How to check if a string contains an other string

This is my file:

#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

When I run below code I get an empty return. That could be because my code doesnt seem to know that Man is in Manchester for example and Sev is in Sevilla. I assume the problem arises in 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)

This is my expected output:

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

Aucun commentaire:

Enregistrer un commentaire