title: Ruby read CSV file as UTF-8 and/or convert ASCII-8Bit encoding to UTF-8
body: <p>I'm using ruby 1.9.2</p>
  
  <p>I'm trying to <strong>parse a CSV file</strong> that contains some French words (e.g. spécifié) and place the contents in a MySQL database.</p>
  
  <p>When I read the lines from the CSV file,</p>
  
  <pre><code>file_contents = CSV.read("csvfile.csv", col_sep: "$")
  </code></pre>
  
  <p>The elements come back as Strings that are <strong>ASCII-8BIT</strong> encoded (spécifié becomes sp\xE9cifi\xE9), and strings like "spécifié" are then NOT properly saved into my MySQL database. </p>
  
  <p><a href="http://yehudakatz.com/2010/05/05/ruby-1-9-encodings-a-primer-and-the-solution-for-rails/">Yehuda Katz</a> says that ASCII-8BIT is really "binary" data meaning that CSV has no idea how to read the appropriate encoding.</p>
  
  <p>So, if I try to make CSV force the encoding like this:</p>
  
  <p><code>file_contents = CSV.read("csvfile.csv", col_sep: "$", encoding: "UTF-8")</code> </p>
  
  <p>I get the following error</p>
  
  <pre><code>ArgumentError: invalid byte sequence in UTF-8: 
  </code></pre>
  
  <p>If I go back to my original ASCII-8BIT encoded Strings and examine the String that my CSV read as ASCII-8BIT, it looks like this "Non sp\xE9cifi\xE9" instead of "Non spécifié".</p>
  
  <p>I can't convert "Non sp\xE9cifi\xE9" to "Non spécifié" by doing this 
  <code>"Non sp\xE9cifi\xE9".encode("UTF-8")</code> </p>
  
  <p>because I get this error: </p>
  
  <p><code>Encoding::UndefinedConversionError: "\xE9" from ASCII-8BIT to UTF-8</code>, </p>
  
  <p>which Katz indicated would happen because ASCII-8BIT isn't really a proper String "encoding".</p>
  
  <p><strong>Questions:</strong></p>
  
  <ol>
  <li><strong>Can I get CSV to read my file in the appropriate encoding?  If so, how?</strong></li>
  <li><strong>How do I convert an ASCII-8BIT string to UTF-8 for proper storage in MySQL?</strong> </li>
  </ol>