I am parsing a tsv file and loading it into MySQL. I got this to work then found there are back slashes in the tsv file that are being interpreted as line breaks. I would like to remove the \ from all fields before the data is sent to the database. This is a shortened example, there are 300 columns in the file and many of them will be blank.
begin
CSV.foreach(file, :col_sep => "\t") do |row|
row.map!{ |e| e.gsub(/\\/, '')}
d = Datafeed.new
d.id = row[0]
d.description = row[1]
d.save!
end
end
When I run this example, I get an error: undefined method `gsub’ for nil:NilClass. I think this error is being generated by blanks in the file. However, when I try adding
row.map!{ |e| unless e.blank e.gsub(/\\/, '') }
it will not execute and I get an error for an unexpected }.
Is this the right direction to eliminate the back slashes? What is the best approach?
Thanks
The
unlessstatement should follow the other code. That’s what is causing the second error. Try this:Note: That code will turn
""intonilwhich may or may not be what you expect.Your approach seems reasonable.
Edit:
To retain the blanks, you can do the following:
or if that’s a bit too much for one line for you, this: