Parsing CSV string in ruby

I have the following line: it is not exactly comma separated, but it has the same effect as the 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 the following to analyze it:

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

but I get the following error:

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

Any idea why this is happening?

I also tried to do response.gsub!("\t", "")to make sure this was a problem, but it didn't seem to help.

+4
source share
3 answers

I worked with #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

conclusion:

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

This will give you every row in the 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"], [], []]

, \n, .

+2

An easy way to fix this is to replace any consecutive space characters with one new line before parsing the line. Then you can use the new line as a line separator, rather than setting it to :auto. This should lead to CSV parsing (since it :autotakes more time to guess your separator), although performance is also negatively affected by the extra call gsub.

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

Source: https://habr.com/ru/post/1568437/


All Articles