As I understood (corrent me in the comments if I am wrong), you have 3 files:
- MySourceFile
- MyKeyFile
- MyOutFile
And you want to:
- Read keys from MyKeyFile
- Read source from MySourceFile
- Iterate over lines in the source
- If line's first word is in keys: append that line to MyOutFile
- Close MyOutFile
So here is the Code:
with open('MySourceFile', 'r') as sourcefile:
source = sourcefile.read().splitlines()
with open('MyKeyFile', 'r') as keyfile:
keys = keyfile.read().split()
with open('MyOutFile', 'w') as outfile:
for line in source:
if line.split():
if line.split()[0] in keys:
outfile.write(line + "\n")
outfile.close()