Create new headers when writing a csv using python
<p>IÂ´m web scrapping different webpages and for each webpage IÂ´m writing each row of the csv file</p>

<pre><code>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
</code></pre>

<p>However, there could be more than one author (so author after web scrapping 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.</p>

<p>Because "Author" is a variable-length list, you should serialize it in some way to fit inside a single field. For example, use a semicolon as a separator.</p>

<p>Assuming you have an <code>authors</code> field with all the authors in them from your <code>webpage</code> object, you would want to change your assignment line to something like this:</p>

<pre><code>variables['Authors']=';'.join(webpage.authors)
</code></pre>

<p>This is a simple serialization of all of the authors. You can of course come up with something else -  use a different separator or serialize to JSON or YAML or something more elaborate like that.</p>

<p>Hopefully that gives some ideas.</p>

python csv