I´m web scraping different webpages and for each webpage I´m writing each row of the csv file
import csv
fieldnames=["Title", "Author", "year"]
counter=1
for webpage of webpages:
if counter==1:
f = open('file.csv', 'wb')
my_writer = csv.DictWriter(f, fieldnames)
my_writer.writeheader()
f.close()
something where I get the information (title, author and year) for each webpage
variables={ele:"NA" for ele in fieldnames}
variables['Title']=title
variables['Author']=author
variables['year']=year
with open('file.csv', 'a+b') as f:
header = next(csv.reader(f))
dict_writer = csv.DictWriter(f, header)
dict_writer.writerow(variables)
counter+=1
However, there could be more than one author (so author after web scraping is actually a list) so I would like to have in the headers of the csv file: author1, author2, author3, etc. But I don't know what would be the maximum number of authors. So in the loop I would like to edit the header and start adding author2,author3 etc depending if in that row is necessary to create more authors.