Reputation: 19
everyone. I finished putting 4 attributes (publicationtitle,publicationurl,publicationdate,publicationdescription) in the first 4 columns in my csv file and also the detailed information below. How can I start putting another 3 attributes in the next 3 columns and their detailed information in the output csv file.(As you can see, the first 4 attributes and second 3 attributes are from different looping system)
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)
Upvotes: 0
Views: 131
Reputation: 1046
See if this link helps How to add a new column to a CSV file using Python?
One suggestion is, if you know how many columns you need to put into the csv file, then construct a list of list or a dictionary variable, to store all your results for each of you loops, and at the end of the script, write a for loop or use DictWriter to output it to your csv file. This link might help. https://docs.python.org/2/library/csv.html#csv.DictWriter
eg (Pseudo code):
my_csv_dict = { 'pub': [], 'work_exp' = [] }
for element in publication_records: my_csv_dict['pub'].append(element)
for element in work_experience_records: my_csv_dict['work_exp'].append(element)
csv_file = csv.DictWriter(file_pointer,fieldnames=my_csv_dict.keys()) csv_file.writerows(my_csv_dict)
Upvotes: 1