2

I was under the assumption that by stating the :col_sep as, say \t

CSV.foreach("items.tsv", :col_sep => "\t", headers: true) do |row|

that columns that include double-quotes would not generate issues, yet the CSV import process still complains about CSV::MalformedCSVError: Illegal quoting in line 45.

How can this be elegantly avoided?

Jerome
  • 5,583
  • 3
  • 33
  • 76

2 Answers2

4

Answer found here. adding a specification for quote character, in this case something non printable:

CSV.foreach("items.tsv", :col_sep => "\t", :quote_char => "\x00", headers: true) do |row|

does the trick. So, apparently defining col_sep is insufficient.

Community
  • 1
  • 1
Jerome
  • 5,583
  • 3
  • 33
  • 76
1

From Ruby 2.4, You can pass liberal_parsing: true option to allow double quotes for methods such as CSV.foreach, CSV.parse, CSV.new.

document is here

https://docs.ruby-lang.org/en/2.5.0/CSV.html

When set to a true value, CSV will attempt to parse input not conformant with RFC 4180, such as double quotes in unquoted fields.

asayamakk
  • 143
  • 2
  • 9