My goal is to write a function that iterates through a CSV file and achieves the following:
- Takes a 'keyword' which is a list of strings and returns all associated codes that have the 'keyword' in the description.
Sample of the CSV file formatting:
"2399","1","theft-bicycle","Bicycle theft"
For example:
- find_that_code(['bicycle'])
- find_that_code(['bicycle' , 'scooter'])
Output:
- [2399]
- [ ]
I am having difficulty figuring out how to frame the IF statement that matches the strings within the keyword list to within row[3].
For the examples, you'll notice that the second example outputs nothing though the string bicycle is present, the string scooter is not.
What I've tried:
def find_that_code(keywords):
codelist = []
keywords = str(keywords)
with open('codes.csv') as csv_file:
reader = csv.reader(csv_file, delimiter=',')
next(reader) # skip the first row
for row in reader:
if row[3] == any([x in keywords for x in keywords]):
code = row[0]
return True
else:
return False
Currently I have the code returning True or False to figure out where my issue is. But once I get how to match the string from the keywords list to row[3] of the CSV then I should be able to finish off the rest of it.
Thank you for your time and I greatly appreciate the advice.
row[3] == any([x in keywords for x in keywords])?