I want to search for elements of namelist in banklist, and if an element is found in banklist, return that element as well as the element after it.
So for example: Namelist: 'John Doe', 'Jack Black'. Banklist: 'John Doe', '9134', 'Richard Pitt', '1652', 'Jack Black', '9145', 'Bob Brand', '6523'
I want this returned: 'John Doe,9134', 'Jack Black,9145'
To this end, I have written the following. It works, but I don't know how to iterate it, so that it searches for all the elements that overlap, instead of stopping after the first.
Result: 'John Doe,9134'. Desired result: 'John Doe,9134', 'Jack Black,9145'.
f = open('namelist.txt', 'r') namestring = f.read() f.close() f = open('testsheet.csv', 'r') bankstring = f.read() f.close() namestring = namestring.replace(", ", ",") namelist = namestring.split(",") banklist = bankstring.split("\n") bankstring = ','.join(banklist) banklist = bankstring.split(",") n = banklist.index(namelist[1]) res = banklist[n] + "," + banklist[n+1] print(res)