Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8694769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:51:49+00:00 2026-06-13T00:51:49+00:00

I exported tables and queries from SQL, where some of the fields are multi-line.

  • 0

I exported tables and queries from SQL, where some of the fields are multi-line.

The Ruby (1.9+) way to read CSV appears to be:

require 'csv'

CSV.foreach("exported_mysql_table.csv", {:headers=>true}) do |row|
    puts row
end

Which works great if my data is like this:

"id","name","email","potato"
1,"Bob","bob@bob.bob","omnomnom"
2,"Charlie","char@char.com","andcheese"
4,"Doug","diggyd@diglet.com","usemeltattack"

(The first line is the header/attributes)

But if I have:

"id","name","address","email","potato"
1,"Bob","--- 
- 101 Cottage row
- Lovely Village
- \"\"
","bob@bob.bob","omnomnom"
2,"Charlie","--- 
- 102 Flame Street
- \"\"
- \"\"
","char@char.com","andcheese"
4,"Doug","--- 
- 103 Dark Cave
- Next to some geo dude
- So many bats
","diggyd@diglet.com","usemeltattack"

Then I get the error:

.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/csv.rb:1894:in `block (2 levels) in shift': Missing or stray quote in line 2 (CSV::MalformedCSVError)

This seems to be because the end of the line doesn’t have a close quote, as it spans several lines.

(I tried ‘FasterCSV’, that gem became ‘csv’ since ruby 1.9)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:51:51+00:00Added an answer on June 13, 2026 at 12:51 am

    Your problem is not the multiline but malformed CSV.

    Replace the \" and end space after a line end like this:

    require 'csv' 
    
    ml = %q{"id","name","address","email","potato" 
    1,"Bob","---  
    - 101 Cottage row 
    - Lovely Village 
    - \"\" 
    ","bob@bob.bob","omnomnom" 
    2,"Charlie","---  
    - 102 Flame Street 
    - \"\" 
    - \"\" 
    ","char@char.com","andcheese" 
    4,"Doug","---  
    - 103 Dark Cave 
    - Next to some geo dude 
    - So many bats 
    ","diggyd@diglet.com","usemeltattack"}
    
    ml.gsub!(/\" \n/,"\"\n").gsub!(/\\\"/,"__")
    
    CSV.parse(ml, {:headers=>true}) do |row|
      puts row
    end
    

    This gives:

    "id","name","address","email","potato"
    1,"Bob","---  
    - 101 Cottage row 
    - Lovely Village 
    - ____
    ","bob@bob.bob","omnomnom"
    etc
    

    If you have no control over the program that delivers the CSV, you have to open the file, read the contents, do a replace and then parse the CSV. I use __ here but you can use other non-conflicting characters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I exported tables and queries from SQL. The ruby (1.9+) way to read csv
I have exported many times tables from Hive to SQL Server. I never face
Using MySQL Administrator GUI tool I have exported some data tables retrieved from an
So I exported two tables in their entirety out of SQL (2005) into an
I've exported a table from a MySQL database, choosing the JSON export option. When
I have exported datatable to Excel successfully. Now I have to change some cell
How to make exported .XLS file Editable Thid code makes .XLS File Read Only
I have a text file exported from a Foxpro (Dos-based) program, but this text
I have a strongly typed ADO.NET dataset containing several tables exported as an XML
I'm in the midst of converting some tables that are using DisplayTag over to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.