Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing csv file in Ruby

I'm expiriencing issue with parsing my CSV file and I cannot resolve this for a while now. My pipe seperated CSV file has a special case that fails the parsing. Here is my code :

CSV.parse(data, {:headers => true, :header_converters => [:symbol, :downcase], :col_sep => '|'}).each do |row|
   if row[:name]
      counter += 1
   end

Here is the case that produces Message: Illegal quoting in line 2 :

|test "Some quoted name"|2|12|Machine|

But this one works and other cases work:

|"Some quoted name"|2|12|Machine|

How do I get passed this one?

like image 595
Gandalf StormCrow Avatar asked Jan 31 '26 20:01

Gandalf StormCrow


1 Answers

That message is technically correct. Quotes have special meaning for the CSV format - they would allow you to embed separator characters in the data. Any quotes used within a field therefore need to be escaped if they are part of the data, or the CSV parser should be informed to use some other character for quoting, in which case it will treat any " that it sees as literal data.

If you don't need to support pipes actually within each field, and have some other unused character you can shift this problem off to, Ruby's CSV can be made to consume your (slightly) malformed csv format:

CSV.parse(data, {:col_sep => '|', :quote_char => "%" })

Otherwise, the correct quoting for your problem line is

|"Some ""quoted name"""|2|12|Machine|
like image 76
Neil Slater Avatar answered Feb 03 '26 08:02

Neil Slater



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!