简体   繁体   English

我如何遍历列表,从列表中提取信息,并将其放入文件中的单独行中

[英]How do I iterate through a list, pull information from the list, and put it into separate lines in a file

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. 我的问题是我需要第二,第三,第四等人的数据成为新的listA,并写在第一人称,第二人等下的行上。因此,信息全部格式化并放置在文件换行。

Structure of the XML file: 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>

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.) 信息重复(如果有帮助,列表的第5个索引将使用不同的变量)。

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']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM