My problem is that I need the data on the second, third, fourth, etc person to become the new listA and be written on the line below the first person, second person, etc etc. So the info is all formatted and placed in the file on a new line.
Structure of the XML file:
<people>
<person>
<fname> Travis </fname>
<lname> Anderson </lname>
<age> 24 </age>
<school> Nebraska </school>
</person>
<person>
<fname> James </fname>
<lname> Kritten </lname>
<age> 23 </age>
<school> Texas State </school>
</person>
<person>
<fname> Kaine </fname>
<lname> Allen </lname>
<age> 27 </age>
<school> Michigan State </school>
</person>
</people>
This is my code thus far:
def peopleData(fileName):
readFile = open(fileName, "r").read()#read file
newFile = input("")#create file
writeFile = open(newFile, "w")#write file
listA = []#list
with open(fileName, "r") as file:
for tags in file:
strippedtags = str(tags.split(">")[1].split("<")[0]) #strip XML tags manually.
listA.append(strippedtags.strip()) #strip ' \n'
listA = list(filter(None, listA)) #get rid of emptyspaces in the list
writeFile.write("{} {}, ".format(listA[1], listA[2])) #fname, lname
writeFile.write("He is {} years old. ".format(listA[3])) #age
writeFile.write("He went to {}.".format(listA[4])+"\n") #school
writeFile.close
so the list would look like
>>>['Travis', 'Anderson', '24', 'Nebraska','James' ,'Kritten', '23', 'Texas State','Kaine', 'Allen', '27', 'Michigan State']
When I execute the function I get the information on the first person and it's exactly how I want it.
"Travis Anderson. He is 24 years old. He went to Nebraska."
But for the rest of the people I don't know how to make them be written in the same way the first person is. Like this.
"Travis Anderson. He is 24 years old. He went to Nebraska."
"James Kritten. He is 23 years old. He went to Texas State."
"Kaine Allen. He is 27 years old. He went to Michigan State."
I need some sort of loop, but I don't know where to start with it.
The information repeats(with different variables every 5th index of the list if that helps.)