title: Using DictWriter to write a CSV when the fields are not known beforehand
body: <p>I am parsing a large piece of text into dictionaries, with the end objective of creating a CSV file with the keys as column headers. </p>
  
  <p><code>csv.DictWriter(csvfile, fieldnames, restval='', extrasaction='raise', dialect='excel', *args, **kwds)</code></p>
  
  <p>The problem arises as the dict for any 'n'th row can include a new, never before used key. I then want the CSV to contain a column for this new key as well. In short, all my fields are not known beforehand so I cannot compile a complete <code>fieldnames</code> at the beginning.</p>
  
  <p>Is there a recommended way to have <a href="http://docs.python.org/2/library/csv.html?highlight=dictwriter#csv.DictWriter" rel="nofollow"><code>csv.DictWriter</code></a> not ignore missing fields but add them to <code>fieldnames</code> instead? Merely changing <code>fieldnames</code> at this point would leave the prior lines with an incorrectly lower number of fields.</p>