I have a TSV file with no quote chars. Whenever a \t
occurs in the data, it is always to separate columns, and never a part of a column value. Whenever a "
occurs, it is always a part of a column value, and never to enclose column values.
I would like to read this CSV in Ruby but it gives me
/Users/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/csv.rb:1925:in `block (2 levels) in shift': Illegal quoting in line 9506. (CSV::MalformedCSVError)
My code is:
CSV.foreach(input_file, { :col_sep => "\t", :headers => true}) do |row|
puts row
end
Any way to get around this problem?
Illegal quoting on lineThis error is caused when there is an illegal character in the CSV file that you are trying to import. To fix this, remember that your CSV file must be UTF-8 encoded. Sometimes, this error is caused by a missing or stray quote.
First, set up the application and seed in some data. Now, in post. rb , declare a method which will be responsible for generating data in CSV format. Depending upon your Rails version and the dependencies added, it's possible you'll need to add a require statement.
Turns out I could fix it by putting quote_char => "\x00"
to trick it into thinking the zero byte is the quote char.
The liberal_parsing
option is available for cases like this. From the documentation:
When set to a true value, CSV will attempt to parse input not conformant with RFC 4180, such as double quotes in unquoted fields.
In your example this would be:
CSV.foreach(input_file, { :col_sep => "\t", :headers => true, :liberal_parsing => true }) do |row|
puts row
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With