I have a large .csv file with a few thousand lines and a handful of columns. For simplicity, it looks a little like
Name, ID
One, 1
Two, 2
I'd like to write a script which takes a list of strings, treating each line as a separate entity, and finds the line containing a match to that entry on the list. Once it's found the line which matches, I'd like to print one of the fields to a new file.
For example, given a list:
1
2
I'd like to be able to produce a file containing the associated 'Name' value.
So far I have no issues with reading in my list file like:
list_file = sys.argv[1]
f = open(list_file, 'r+')
ListOfNames = f.read()
But I'm a little confused about how to proceed. Previously, when I've written similar codes, I've prompted the user for a string using raw_input() and simply searched through the csv file for a match like so:
def Retu_Name(ID):
file_csv = csv.DictReader(open('file.csv'))
for row in file_csv:
if row['ID'] == ID:
print row['Name']
But I'm unsure of how to do this whilst iterating over a list. Would anyone be able to help?
Apologies that this question isn't the most structured, but any help would be appreciated.
Cheers
