title: Missing or stray quote in line 1 (CSV::MalformedCSVError)
body: <p>I'm having issues importing this CSV file in ruby/rails</p>
  
  <p>The error message I'm getting is this:</p>
  
  <pre><code>Missing or stray quote in line 1 (CSV::MalformedCSVError)
  </code></pre>
  
  <p>But I'm not sure what's happening because my CSV looks perfectly fine.  Here's sample data below:</p>
  
  <pre><code>"lesley_grades","lesley_id","last","first","active","site","cohort","section","sections_title","faculty","completed_term_cred","term","sec_start_date","sec_end_date","grade","stc_cred","active_program","most_recent_program","intent_filed","stc_term_gpa","sta_cum_gpa","start_term","prog_status","last_change_date"
  ,1234456,John,Doe,TRUE,"Baltimore, MD",0002012,14/FA_ERLIT_6999_U15AA,Directed Independent Study,"Jane Hicks , Jill Saunders",2,14/FA,9/3/14,12/17/14,B-,2,EME.2270.TCBAL.01,EME.2270.TCBAL.01, ,3.3,3.148,12/SU,A,9/2/14
  ,1234455,John,Doe,TRUE,"Baltimore, MD",0002012,14/FA_ERSPD_6999_U15AG,Directed Independent Study,"Jane Hicks , Jill Saunders",3,14/FA,9/3/14,12/17/14,A-,3,EME.2270.TCBAL.01,EME.2270.TCBAL.01, ,3.3,3.148,12/SU,A,9/2/14
  </code></pre>
  
  <p>To give context, effectively the csv looks like this, with the lesley_grades as the first column.  The over CSV script file will look for the first column and check that active an Active Record object, then it stores it the db with that exact same model name, assuming all migrations are pre-set.</p>
  
  <pre><code>lesley_grades   lesley_id   last   first    active  
                   1234556    Doe    John     TRUE    
                   1123445    Doe    John     TRUE
  </code></pre>
  
  <p>Here's part of the code that's causing me issues</p>
  
  <pre><code>def import!(csv)
   csv_reader = CSV.parse(csv)
   ActiveRecord::Base.transaction do
    csv_reader.each do |row|
      set_record_class_and_columns(row) if header_row?(row)
  
      if columns_mapping_defined? &amp;&amp; record_class_defined? &amp;&amp; record_row?(row)
        import_row(row)
      end
    end
    if imports_failed?
      puts 'Aborting importing and rolling back...'
      show_errors
      raise ActiveRecord::Rollback
    end
  end
  </code></pre>
  
  <p>end</p>
  
  <p>It can't get passed this line <code>csv_reader = CSV.parse(csv)</code></p>
  
  <p>before I put the quotes in the headers I was getting this error</p>
  
  <p><code>Unquoted fields do not allow \r or \n (line 1). (CSV::MalformedCSVError)</code></p>
  
  <p><strong><em>UPDATE</em></strong></p>
  
  <p>The CSV gets started from the command line like this:</p>
  
  <pre><code>rails runner scripts/import_csv.rb &lt; lesley_grades.csv
  </code></pre>
  
  <p>which then gets initialized here </p>
  
  <pre><code>CSVImporter.new.import!($stdin)
  </code></pre>
  
  <p>But as @smathy suggests I changed the method to CSV.parse(csv.gsub /\r/, '')</p>
  
  <p>but now the <code>def import!</code> method to take in a <code>gsub</code> block produces this error</p>
  
  <pre><code>in `import!': undefined method `gsub' for #&lt;IO:&lt;STDIN&gt;&gt; (NoMethodError)
  </code></pre>
  
  <p>Not sure how to make CSV an object?</p>
  
  <p>Any suggestions or refactoring to make this work?
  Thanks all</p>