title: How to save numpy array of Strings (with commas) to CSV?
body: <p>tl;dr ANSWER: Don't use numpy. Use <code>csv.writer</code> instead of <code>numpy.savetxt</code>.</p>
  
  <p>I'm new to Python and NumPy. It seems like it shouldn't be so difficult to save a 2D array of strings (that contain commas) to a CSV file, but I can't get it to work the way I want.</p>
  
  <p>Let's say I have an array that looks like this (made from a list of lists):</p>
  
  <pre><code>[['text1, text2', 'text3'],
  ['text4', 'text5']]
  </code></pre>
  
  <p>I want a CSV file that looks like this (or without quote characters) in Excel (pipe = cell separator):</p>
  
  <pre><code>'text1, text2' | 'text3'
  'text4'        | 'text5'
  </code></pre>
  
  <p>I'm using <code>numpy.savetxt(filename, array, fmt="%s")</code>, and I get the following CSV output (with square brackets):</p>
  
  <pre><code>['text1, text2','text3']
  ['text4','text5']
  </code></pre>
  
  <p>Which displays in Excel like this:</p>
  
  <pre><code>['text1  | text2' | 'text3']
  ['text4' | 'text5']
  </code></pre>
  
  <p>I tried fussing with the savetxt delimiter argument, but no change in output. </p>
  
  <p>Do I need to do this manually? If so, let me know if there are any shortcuts I should be aware of. </p>
  
  <p>Ultimately, I need to import the CSV into a Postgresql database. I'm not completely clear on exactly what the CSV formatting needs to be for this to work as expected, but I'm assuming if it looks wrong in Excel, it will probably end up messed up in Postgres. The <a href="http://www.postgresql.org/docs/9.3/static/sql-copy.html" rel="nofollow">Postgres documentation</a> says:</p>
  
  <blockquote>
    <p>The values in each record are separated by the DELIMITER character. If
    the value contains the delimiter character, the QUOTE character, the
    NULL string, a carriage return, or line feed character, then the whole
    value is prefixed and suffixed by the QUOTE character, and any
    occurrence within the value of a QUOTE character or the ESCAPE
    character is preceded by the escape character. You can also use
    FORCE_QUOTE to force quotes when outputting non-NULL values in
    specific columns.</p>
  </blockquote>
  
  <p>Thanks!</p>
  
  <p>++++++++++++++++++++++++++++</p>
  
  <p>Real input and output, in case it's relevantly different:</p>
  
  <p>array:</p>
  
  <pre><code>[['8908232', 'Plant Growth Chamber Facility at the Department of Botany, University of Wisconsin-Madison', 'DBI', 'INSTRUMENTAT &amp; INSTRUMENT DEVP', '1/1/90', '12/19/89', 'WI', 'Standard Grant', 'Joann P. Roskoski', '12/31/91', '$94,914.00 ', 'BIO', '1108', '', '$0.00 ']]
  </code></pre>
  
  <p>CSV output:</p>
  
  <pre><code>['8908232', 'Plant Growth Chamber Facility at the Department of Botany, University of Wisconsin-Madison', 'DBI', 'INSTRUMENTAT &amp; INSTRUMENT DEVP', '1/1/90', '12/19/89', 'WI', 'Standard Grant', 'Joann P. Roskoski', '12/31/91', '$94,914.00 ', 'BIO', '1108', '', '$0.00 ']
  </code></pre>
  
  <p>Excel's version:</p>
  
  <pre><code>['8908232'   'Plant Growth Chamber Facility at the Department of Botany  University of Wisconsin-Madison'    'DBI'   'INSTRUMENTAT &amp; INSTRUMENT DEVP'    '1/1/90'    '12/19/89'  'WI'    'Standard Grant'    'Joann P. Roskoski'     '12/31/91'  '$94   914.00 '     'BIO'   '1108'  ''  '$0.00 ']                  
  </code></pre>