title: Scraping a table into a dict and writing each row to CSV
body: <p>I have a project that was using Scraperwiki to write to their sqlite store, but I need to just write a <a href="http://docs.python.org/2/library/csv.html" rel="nofollow">CSV</a>. The catch is that all the data is stored in a dict, which works fine for writing to sqlite:</p>
  
  <pre><code>scraperwiki.sqlite.save(unique_keys = ['somekey'], data = data, table_name='fancy')
  </code></pre>
  
  <p>I run that after I scrape each row. There's no order to a dict so I can't just write the values to CSV. I've been looking over <a href="http://docs.python.org/2/library/csv.html#csv.DictWriter" rel="nofollow">csv.DictWriter</a> and <a href="http://docs.python.org/2/library/collections.html#defaultdict-objects" rel="nofollow">collections.defaultdict</a>, and I'm still wrapping my head around how I'd refactor my code so that I can write data, which is a dictionary to CSV instead. Here's an example of my code as structured now:</p>
  
  <pre><code>def store_exception(exception, line_number, some_string):
      data = {
          'timestamp'  : datetime.now(),
          'line_number': line_number,
          'message'    : exception,
          'string'     : some_string
      }
      scraperwiki.sqlite.save(unique_keys = ['timestamp'], data = data, table_name='error_log')
  </code></pre>
  
  <p>I think I want something like this though:</p>
  
  <pre><code>def store_exception(exception, line_number, some_string):
      data = {
          'timestamp'  : datetime.now(),
          'line_number': line_number,
          'message'    : exception,
          'string'     : some_string
      }
      d = defaultdict(lambda: "")
      d_order = d['timestamp'],d['line_number'],d['message'],d['string']
  
      with open('some/path.csv', 'w') as csvfile:
          linewriter = csv.DictWriter(csvfile, d_order, delimiter='|',
                              quotechar='"', quoting=csv.QUOTE_MINIMAL)
          linewriter.writerow(data)
  </code></pre>
  
  <p>That seems inefficient, though. Do I need both collections.defaultdict and csv.DictWriter?</p>