昨天在stackoverflow上问了第一个问题,被鄙视了。。。说“This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form”。。。
问题如下:
I have a csv file with header. I need to update all values in a specific column but keep the header, and then save the file. How can I do it via csv library?
require 'csv'
file_name = "path/to/file.csv"
CSV.foreach(file_name, {:headers=>true}) do |row|
puts row[4]
end
arr = []
#
CSV.foreach(file_name, {:headers=>true}) {|row| arr << row}
arr.each do |row|
row[4].replace("new program name")
end
#
puts arr
##
CSV.open(file_name, "wb", {:headers=>true}) do |csv|
csv << ["your", "header", "value"]
arr.each {|row| csv << row}
end
##
CSV.foreach(file_name, {:headers=>true}) do |row|
puts row[4]
end