9
我需要读取CSV文件,更新字段,然后保存更改。我有一切工作正常,除了保存我的更改到我正在更新的字段:如何解析CSV文件,更新字段,然后保存
require 'csv'
@parsed_file = CSV::Reader.parse(File.open("#{RAILS_ROOT}/doc/some.csv"))
@parsed_file.each_with_index do |row, x|
address = row[5]
l = Location.address_find(address)
if l != nil
puts "#{l.name} at #{l.address}"
row[14] = l.store_code
puts row[14]
else
puts "No matching address Found!!!"
end
#What do I do here? Something like this? CSV::Writer.generate(@parsed_file)
end
我在这里做什么?如何保存我所做的更改并更新文件?
我会csv_out.close外循环? – thatmiddleway 2010-08-24 22:10:29
是的,'csv_out.close'将会在'each_with_index'循环之外,因为你只需要这样做一次,并且在循环完成原始文件中的行之后。 – mikej 2010-08-24 22:12:24
这很有道理,谢谢! – thatmiddleway 2010-08-24 22:13:03