Update a CSV file : csv « Development « Ruby






Update a CSV file


require 'csv'

File.open("newdata.csv","w") do |file|
  IO.foreach("data.csv") do |line|
     a=CSV::parse line.chomp
     if a[1].to_i > 20
       s=a[2].to_i
       a[2]=s*1.1  # 10% raise
     end
    file.puts CSV::create(a)
  end
end

 








Related examples in the same category

1.Read CSV file
2.Parse one CSV file line
3.Create data for CSV file
4.Reading and Searching CSV Data
5.use CSV alongside the File class
6.loading the data from a CSV-formatted file into an array is with CSV.read:
7.pick out the first person in the data called Laura:
8.find the people in your database whose ages are between 20 and 40:
9.Saving Data Back to the CSV File