我的问题是我需要第二个,第三个,第四个等人的数据成为新的listA并写在第一个人,第二个人等下面的行上。所以信息都是格式化的放在新行上的文件中。
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>
到目前为止,这是我的代码:
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
所以列表看起来像
>>>['Travis', 'Anderson', '24', 'Nebraska','James' ,'Kritten', '23', 'Texas State','Kaine', 'Allen', '27', 'Michigan State']
当我执行该功能时,我会获得第一个人的信息,这正是我想要的。
"Travis Anderson. He is 24 years old. He went to Nebraska."
但对于其他人,我不知道如何让他们以与第一个人相同的方式写作。像这样。
"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."
我需要某种循环,但我不知道从哪里开始。
重复信息(如果有帮助的话,每隔5个索引使用不同的变量。)
答案 0 :(得分:2)
/* new parts of the css */
#header {
min-height:112px; /* in case user data is made smaller */
padding:10px 10px 0 20px;
position:relative;
}
#logo {
width: 210px;
position:absolute;
top:50%;
width:210px;
height:62px;
left:20px;
margin-top:-32px;
z-index:1; /* bring logo above the user data */
}
#navigation {
position:absolute;
bottom:0;
left:210px;
font-size: 20px;
height: 40px;
z-index: 1; /* bring navigation above the user data*/
}
#userInfo table{
margin:0 0 0 auto;
}
.headRight{
text-align: right;
padding-bottom: 0.2em;
}
出:
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))
并显示库有多简单,我用两行代码提取列表中的所有文本:
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.
出:
from bs4 import BeautifulSoup
soup = BeautifulSoup(xml, 'lxml')
[i for i in soup.stripped_strings]