Reputation: 3
Use case: The user enters an ID code. A stored file contains information about people, one line per person, including their ID codes. I need to find this user's line in the file and append that to another text file.
with open("file.txt") as file:
for line in file:
if id_code in line:
yield line
Now what? How do I go from yielding the line to getting it in another file? Also, if the id code isn't in the file at all, can I ask them to try again?
Upvotes: 0
Views: 43
Reputation: 10090
Without the actual txt file, it's hard to say if this will work perfectly, but it's the right general idea.
with open("file.txt", "r") as f, open("otherfile.txt", "a") as g:
for line in file.readlines():
lineparts = line.split()
if id_code in lineparts:
g.write(line)
Upvotes: 1