1

I have a CSV like this for example:

ACCOUNT:STATUS:FIRST_NAME:LAST_NAME
12345:1:My "Problematic" Name:Last Name

If I use CSV.foreach I get message: Illegal quoting in line 2

How can I handle this correctly and still preserve quotation marks in the string that I read?

My current code:

CSV.foreach("accounts_data.csv", { :col_sep => ':', headers: true } ) do |account|
end
Tians
  • 443
  • 1
  • 5
  • 14
  • For CSV to eat your line, it should be this: `12345:1:"My ""Problematic"" Name":Last Name`. If your file is not too big, you can do it manually. – Sergio Tulentsev Dec 30 '15 at 08:16

1 Answers1

1

You can set the options to specify col_sep as your file uses colon(:) as separator, and quote_char to single quote just to get around double quote issue.

CSV.foreach("/path/to/file", {quote_char: "'", col_sep: ":"}) do |row|
  p row
end

Output:

["ACCOUNT", "STATUS", "FIRST_NAME", "LAST_NAME"]
["12345", "1", "My \"Problematic\" Name", "Last Name"]
Wand Maker
  • 18,476
  • 8
  • 53
  • 87