So I have a CSV with two columns that contain dollar amounts in string format. head -n 5 file.csv reveals the following:
Title,Distributor Long Name,Wk,Estimated Weekend Gross,Cume,Locs Reported,Avg/Loc,Booking Title #
"=""Zero Dark Thirty""","=""Sony""",4,"24,000,000","29,480,807",2937,"8,172","=""66273"""
"=""Haunted House, A""","=""Open Road""",1,"18,817,000","18,817,000",2160,"8,712","=""71209"""
"=""Gangster Squad""","=""Warner Bros.""",1,"16,710,000","16,710,000",3103,"5,385","=""66556"""
"=""Django Unchained""","=""The Weinstein Company""",3,"11,065,000","125,399,122",3012,"3,674","=""66122"""
This goes on for about 40 rows. You'll notice two of the columns — the "Estimated Weekend Gross" and "Cume" ones — have their values as strings.
So my question is, is there a way to iterate over only these two columns, convert the string values to integers doing something like row.to_s.gsub(',','').to_i and then overwrite those values to their respective rows in the same CSV?
I tried doing something like this, but I'm not getting a properly formatted CSV..
File.open('modified.csv', 'w') do |csv|
CSV.foreach('original.csv') do |row|
csv << row[0].to_s.gsub('=','').gsub(', The','')
csv << row[3].to_s.gsub(',','').to_i
csv << row[4].to_s.gsub(',','').to_i
end
end
I've also played around with :headers => :integer when doing the block, but it won't let me convert the values from strings to integers. So, what am I missing? Should I store these values and then write a new CSV or is there a simpler way?