locoboy
locoboy

Reputation: 38960

Parsing CSV string in ruby

I have the following string - it's not exactly comma separated but has the same effect as a csv dataset:

response = "Date;Amount;Account;User\n2014-12-01;12.01;abcxyz;user1\n2014-12-01;10.09;fine;user2\n\r\n\t\t\r\n"

I tried running the following to parse it:

CSV.parse(response, :col_sep => ";", :row_sep => :auto) 

but I get the following error:

CSV::MalformedCSVError: Unquoted fields do not allow \r or \n

Any idea why this would be happening?

I also tried doing a response.gsub!("\t", "") to see if that was the issue, but it didn't seem to help.

Upvotes: 15

Views: 11815

Answers (3)

roob
roob

Reputation: 1136

This will give you each row in an array.

CSV.parse( response.gsub( /[\r\t]/, '' ), col_sep: ";" )
=> [["Date", "Amount", "Account", "User"], ["2014-12-01", "12.01", "abcxyz", "user1"], ["2014-12-01", "10.09", "fine", "user2"], [], []]

Unless you want to merge all rows into a single line, you need to leave the \n for the parser to interpret as a new row.

Upvotes: 6

Nicky McCurdy
Nicky McCurdy

Reputation: 19573

An easy way to fix this is to replace any consecutive whitespace characters with a single newline before you parse the string. Then you can use the newline as your row separator, instead of setting it to :auto. This should make CSV parsing faster (since it takes more time for :auto to guess your delimiter), though performance is technically also negatively affected by the additional call to gsub.

CSV.parse(response.gsub(/\s+/, "\n"), col_sep: ';', row_sep: "\n")

Upvotes: 1

Arup Rakshit
Arup Rakshit

Reputation: 118299

I got it work with the use of #strip :

require 'csv'

response = "Date;Amount;Account;User\n2014-12-01;12.01;abcxyz;user1\n2014-12-01;10.09;fine;user2\n\r\n\t\t\r\n"

CSV.parse(response.strip, :col_sep => ';') do |row|
  p row
end

output :

arup$ ruby a.rb
["Date", "Amount", "Account", "User"]
["2014-12-01", "12.01", "abcxyz", "user1"]
["2014-12-01", "10.09", "fine", "user2"]

Upvotes: 16

Related Questions