William Lim
William Lim

Reputation: 3

How to remove character ',' in csv file

CSV.foreach("C:/Users/StocksCSV/XOM/XOM.N_BAL_ANN.csv",:col_sep => ';', :skip_blanks => true) do |row|


row.gsub(/[^,]/,"")
#puts row.inspect


ANN_BAL_TBL.insert(:company_name => 'XOM', :statement_title => row[0], :item1 => row[1],
                    :item2 => row[2], :item3 => row[3], :item4 => row[4], :item5 => row[5])

end
puts "ANN_BAL_TBL DB Row Count: #{ANN_BAL_TBL.count}"

This causes the error:

"undefined method `gsub' for #<Array:0x26d4bc0> (NoMethodError)"

My CSV file is as follows (I just want to remove the "," in the currency)

" In Millions of U.S. Dollars (except for per share items);2012 2012-12-31;2011 2011-12-31 Reclassified 2012-12-31;2010 2010-12-31;2009 2009-12-31;2008 2008-12-31

Cash & Equivalents;9,582.0;12,664.0;7,825.0;10,693.0;31,437.0 "

Upvotes: 0

Views: 231

Answers (1)

Sam Starling
Sam Starling

Reputation: 5378

Try this:

row = row.map { |i| i.gsub(',', '') }

What that'll do is call gsub on every item in the array, and it'll work as long as each item is a string. What you're trying to do is call gsub on an array, which isn't possible.

Upvotes: 1

Related Questions