-1

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.)

thatoneguy
  • 73
  • 6
  • Well I need help so I am going to ask questions. I've been at this going on 11 hours now. – thatoneguy Dec 10 '16 at 04:00
  • 1
    @thatoneguy take a `break;` – Stargateur Dec 10 '16 at 04:01
  • I don't want to, I want to figure this out. I want to learn how to code and the best way for me to do that is to code a lot. – thatoneguy Dec 10 '16 at 04:02
  • I like the use of your coding pun though. – thatoneguy Dec 10 '16 at 04:03
  • Could you depict the structure of your XML file? The code is intuitive but it can made extremely simpler with the XML library. – Ébe Isaac Dec 10 '16 at 04:06
  • 1
    Coding a lot is certainly one way of getting better at coding, but it only applies to the people actually doing the coding, viz, the people who are answering your questions. You might try looking for guides or blogs on strategies for debugging your code. – TigerhawkT3 Dec 10 '16 at 04:08
  • I put how to tags look from the XML file. Also, I did play with the xml.etree stuff on an earlier practice project. But I want to try it manually to figure out some of the workings. – thatoneguy Dec 10 '16 at 04:09
  • TigerhawkT3 I've only been asking questions because I have nobody to go to other than here. I always look for an answer, or try to figure out an answer before asking a question. – thatoneguy Dec 10 '16 at 04:10
  • Sorry, but you didn't provide the complete XML structure. Could you expand it? – Ébe Isaac Dec 10 '16 at 04:12
  • I expanded it to the whole file, it's a very basic XML structure. Nothing too fancy. – thatoneguy Dec 10 '16 at 04:16
  • at least post complete XML code – 宏杰李 Dec 10 '16 at 04:16
  • I did now, sorry. – thatoneguy Dec 10 '16 at 04:19
  • If you want learn you should try to answer yourself with this http://stackoverflow.com/questions/1912434/how-do-i-parse-xml-in-python. Don't hesitate to purge old comment – Stargateur Dec 10 '16 at 04:21

1 Answers1

2
xml = '''
<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>'''


from bs4 import BeautifulSoup
soup = BeautifulSoup(xml, 'lxml')
for p in soup.find_all('person'):
    fullname = p.fname.text.strip() + p.lname.text.rstrip()
    age = p.age.text.strip()
    school = p.school.text.strip()
    print("{}. He is {} years old. He went to {}.".format(fullname, age, school))

out:

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.

and to show how simple the library is, i extract all text in a list in two lines code:

from bs4 import BeautifulSoup
soup = BeautifulSoup(xml, 'lxml')
[i for i in soup.stripped_strings]

out:

['Travis', 'Anderson', '24', 'Nebraska', 'James', 'Kritten', '23', 'Texas State', 'Kaine', 'Allen', '27', 'Michigan State']
宏杰李
  • 11,820
  • 2
  • 28
  • 35
  • Thanks, I'll have to try that out. Is there any way to do it manually, or would that be too complicated? Just asking. – thatoneguy Dec 10 '16 at 04:28
  • 1
    manually parse a xml file is too complicated, there a a lot good library can do this work for you. this code is very simple, you can read the doc in half an hour and write the some code. – 宏杰李 Dec 10 '16 at 04:31
  • I mean to manually do the output through loops without librarys. I'm just wondering if it is a possibility so I can have something to do after playing around with the beautiful soup library. – thatoneguy Dec 10 '16 at 04:33
  • i update the code to extract the data in a list, you can do whatever you want – 宏杰李 Dec 10 '16 at 04:38