So I have multiple txt documents that are formatted like this:
james
M
18
72
170
teresa
F
19
63
115
Some have only two names, others have 50. Basically, what I'm trying to do is format the info so that it is rewitten to a new text file with the following format:
Name: james
Gender: M
Age: 18
Height: 72
Weight: 170
Name: teresa
Gender: F
Age: 19
Height: 63
Weight: 115
So far I have:
def tagInfo(fileName):
with open(fileName) as infile, open("altered.txt","w") as outfile:
for i,line in enumerate(infile):
if i == 0:
outfile.write("Name: "+line.strip()+"\n")
if i == 1:
outfile.write("Gender: "+line.strip()+"\n")
if i == 2:
outfile.write("Age: "+line.strip()+"\n")
if i == 3:
outfile.write("Height: "+line.strip()+"\n")
if i == 4:
outfile.write("Weight: "+line.strip()+"\n")
outfile.close()
All this program does is edit the first 5 lines. I'm trying to make it so if I give it a list of 50 people to change, it changes the info of all 50 people, not just the first five lines. With my current solution, that's not possible. I'm not sure where to go from here and I feel like it may be best to scrap everything and take a different approach.
Do you guys have any solutions?