reading in from csv files

16 views
Skip to first unread message

fugee ohu

unread,
Jun 14, 2018, 7:09:48 PM6/14/18
to Ruby on Rails: Talk
csv files have their commas pre-escaped so i don't have to worry about there being commas in data, i can just use the split function to read in?

Hassan Schroeder

unread,
Jun 15, 2018, 12:02:46 AM6/15/18
to rubyonrails-talk
On Thu, Jun 14, 2018 at 4:09 PM, fugee ohu <fuge...@gmail.com> wrote:
> csv files have their commas pre-escaped so i don't have to worry about there
> being commas in data, i can just use the split function to read in?

Maybe.

-----------------------------------------------------------------------------------------
require 'csv'

csv_string = CSV.generate do |csv|
csv << ['dog', 'barking\, all night', 'woof\n']
csv << ['cat', 'meowing incessantly', 'meow\n']
end

csv_string.split("\n").each { |line| puts("# of fields:
#{line.split(',').length}\n") }
-----------------------------------------------------------------------------------------

Maybe not. 😀

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
twitter: @hassan
Consulting Availability : Silicon Valley or remote
Message has been deleted

fugee ohu

unread,
Jun 15, 2018, 7:36:35 AM6/15/18
to Ruby on Rails: Talk
 So then right, this isn't the desired result
# of fields: 4
# of fields: 3
I tried adding more backslashes to 'barking\, all night' barking\\, barking\\\, barking \\\\, none of them got different results they all said 4 and 3
Reply all
Reply to author
Forward
0 new messages