2016-12-10 72 views
-1

我的問題是,我需要第二,第三,第四等人的數據成爲新的列表A和寫在第一個人,第二個人等等下面的行中,所以信息全部格式化並放在一個新行中的文件中。 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

那麼我需要幫助,所以我會問問題。我現在已經有11個小時了。 – thatoneguy

+1

@thatoneguy休息;' – Stargateur

+0

我不想,我想弄清楚。我想學習如何編寫代碼,而且我最好的方法就是編寫很多代碼。 – thatoneguy

回答

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

出來:

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] 

出來:

['Travis', 'Anderson', '24', 'Nebraska', 'James', 'Kritten', '23', 'Texas State', 'Kaine', 'Allen', '27', 'Michigan State'] 
+0

輸出應使用「報價格式」。 – Stargateur

+0

謝謝,我將不得不嘗試一下。有沒有辦法手動執行,還是會太複雜?只是問問。 – thatoneguy

+0

只是python顯示,但我會更新 –

相關問題