Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9128939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:38:03+00:00 2026-06-17T07:38:03+00:00

So I have a CSV with two columns that contain dollar amounts in string

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:38:04+00:00Added an answer on June 17, 2026 at 7:38 am

    Aaron, just change the row and write it to your new file like this

    require 'csv'
    
    File.open('modified.csv', 'w') do |csv|
      CSV.foreach('original.csv', :headers => true) do |row|
        row['Estimated Weekend Gross'] = row['Estimated Weekend Gross'].delete(',').to_i
        row['Cume'] = row['Cume'].delete(',').to_i
        csv << row
      end
    end
    

    EDIT: if you want to save the headers in modified.csv you can do it like this, but there must be a shorter way without opening the file twice, if someone has a better solution for this ?

    headers = CSV.open('original.csv', 'r', :headers => true).read.headers
    CSV.open('modified.csv', 'w') do |csv|
      csv << headers
      CSV.foreach('original.csv', :headers => true) do |row|
        row['Estimated Weekend Gross'] = row['Estimated Weekend Gross'].delete(',').to_i
        row['Cume'] = row['Cume'].delete(',').to_i
        csv << row
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have .csv file that contain 2 columns delimited with , . file.csv word1,word2
I have two groups of files that contain data in CSV format with a
I have a CSV file with two columns: cat @ c a t dog
Say I have loaded a csv file into R with two columns (column A
I have to compare entries from a csv file that might contain more than
I have a CSV file that I'm totaling up two ways: one using Excel
I have a csv file which has two columns, a numeric ID ( IDVAR
I have two csv file ( ) I can get it to print out
I have two csv file. First File has date offerid clicks orders Second File
I have CSV files that have multiple columns that are sorted. For instance, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.