每個人。我完成了在我的csv文件的前4列中的4個屬性(publicationtitle,publicationurl,publicationdate,publicationdescription)以及下面的詳細信息。我怎樣才能開始將在接下來的3列其他3個屬性及其詳細信息在輸出csv文件。(正如你可以看到,第一個4個屬性和第二3個屬性都來自不同的循環體系)python csv輸出與特定列
import csv
from bs4 import BeautifulSoup
fconn=open('D:\\Resumes\\Resume1.html')
html=fconn.read()
fconn.close()
tree=BeautifulSoup(html)
publication=tree.findAll('div',{'class':'publication-section'})
with open('D:\\ResumesClassification\\test.csv', 'wb') as csvfile:
publicationwriter=csv.writer(csvfile,dialect='excel')
publicationwriter.writerow(['publicationtitle']+['publicationurl']+['publicationdate']+['publicationdescription'])
for i in publication:
publicationtitle=i.find('p',{'class':'publication_title'})
if publicationtitle!=None:
publicationtitle=publicationtitle.text
publicationtitle=publicationtitle.encode('ascii','ignore')
else:
publicationtitle="publication title not metioned"
......
publicationwriter.writerow([publicationtitle,publicationurl,publicationdate,publicationdescription])
workexperience=tree.findAll('div',{'class':'work-experience-section'})
for i in workexperience:
.....(just like the publication stuff, there are titles, dates and descriptions)
在你的兩個循環中建立一個字典,然後使用[csv.dictwriter()](https://docs.python.org/2/library/csv。 html#csv.DictWriter)寫入文件 – SiHa