title: Writing to CSV, getting "Error: need to escape" for a blank string
body: <p>I am probably going to feel very dumb when someone spots what I'm doing wrong here, but I am finding myself unable to defeat what looks like it should be a simple error.</p>
  
  <p>I am writing some data to a CSV with Python. One of the things I want to write is a <code>list</code> of integers. I <code>join</code> the list into a string before writing it to the file:</p>
  
  <pre><code>with open('publishers.csv', 'wb') as f:
      writer = csv.writer(f, quoting=csv.QUOTE_NONE, delimiter='|', quotechar='')
      for item in big_list_of_objects:
          description = item.description
          number_list = item.number_list
          formatted_numbers = "-".join(number_list)
          writer.writerow([
              description,
              formatted_numbers
              ])
  </code></pre>
  
  <p><code>number_list</code> may have anywhere from zero to a whole bunch of numbers in it. If it's an empty list, the <code>join</code> just sets <code>formatted_numbers</code> equal to a blank string. If it's not an empty list, I get a string made of up integers connected by hyphens.</p>
  
  <pre><code>number_list = [1,2,34,12]
  formatted_numbers = '1-2-34-12'
  
  number_list = []
  formatted_numbers = ''
  </code></pre>
  
  <p>That's the idea, anyway. In reality, what happens is the first five rows write successfully then I get:</p>
  
  <pre><code>File "&lt;console&gt;", line 1, in &lt;module&gt;
    File "/path/path/path.py", line 500, in offending_function
      formatted_numbers
  Error: need to escape, but no escapechar set
  </code></pre>
  
  <p>Now in this particular situation, the first five rows that write successfully have an empty <code>number_list</code>. The row that consistently crashes <em>also</em> has an empty <code>number_list</code>. There is nothing weird about the value being written immediately before or after <code>number_list</code> on this row. And there is nothing weird about the <code>formatted_numbers</code> being written when this error crops up - I tossed in a <code>print</code> statement to debug, and it's just an empty string like the five before it.</p>
  
  <p>Can anyone help me figure out where I might be going wrong here?</p>
  
  <hr>
  
  <p>Edit: I have added these print statements:</p>
  
  <pre><code>with open('publishers.csv', 'wb') as f:
      writer = csv.writer(f, quoting=csv.QUOTE_NONE, delimiter='|', quotechar='')
      for item in big_list_of_objects:
          description = item.description
          print "Description for %r is %r" % (item,description)
          number_list = item.number_list
          print "Now formatting %r for %r" % (number_list,item)
          formatted_numbers = "-".join(number_list)
          print repr(formatted_numbers)
          writer.writerow([
              description,
              formatted_numbers
              ])
  </code></pre>
  
  <p>The result:</p>
  
  <pre><code>Description for 'p89' is u''
  Now formatting '' for 'p89'
  ''
  Description for 'p88' is u''
  Now formatting '' for 'p88'
  ''
  Description for 'p83' is u''
  Now formatting '' for 'p83'
  ''
  Description for 'p82' is u'in-tr-t91411'
  Now formatting '' for 'p82'
  ''
  Description for 'p81' is u''
  Now formatting '' for 'p81'
  ''
  Traceback (most recent call last):
    File "&lt;console&gt;", line 1, in &lt;module&gt;
    File "/path/path/path.py", line 501, in offending_function
      formatted_numbers
  Error: need to escape, but no escapechar set
  </code></pre>
  
  <p><code>p81</code> is not written to the CSV - this is where the crash occurs. However, as you can see, <code>print repr(formatted_numbers)</code> reveals it to be a blank string identical to those before it. There is no <code>description</code> for item <code>p81</code> (just a blank string), but there <em>is</em> a <code>description</code> for the item preceding it. </p>