Jerome
Jerome

Reputation: 6189

Rails CSV import without double-quote hiccups

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?

Upvotes: 3

Views: 1329

Answers (2)

asayamakk
asayamakk

Reputation: 163

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.

Upvotes: 2

Jerome
Jerome

Reputation: 6189

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.

Upvotes: 4

Related Questions