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.
response.gsub!("\t", "")
I worked with #strip:
#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"]
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, .
\n
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.
:auto
gsub
CSV.parse(response.gsub(/\s+/, "\n"), col_sep: ';', row_sep: "\n")
Source: https://habr.com/ru/post/1568437/More articles:How to use HTML import in Safari with .js web components - htmlminify html in golang to remove extra spaces and characters for the next line - goRemove duplicates in foreach - phpAndroid animation to rotate a view and freeze it after rotation - androidHow to delay send commands in AutoHotKey? - autohotkeySOLR: solrQuery.addDateRangeFacet (): cannot add space% 2B1DAY to the value for the field: - solr4Android Studio (IntelliJ) error message - javaКак скомпилировать несколько файлов typescript в один файл javascript с помощью Gulp? - javascriptCombining a non-static method in std :: function with the "this" parameter using as little code as possible - c ++Vertical bootstrap menu and angular - javascriptAll Articles