https://dalibornasevic.com/posts/68-processing-large-csv-files-with-ruby
CSV.foreach 效率和占用内存方面都相对要好
# 修改原文件中Email列字段,并保存为新csv文件
write_parameters = { write_headers: true, headers: ["Id","Score","Text","Ip Address","Email","External Id","Response Date","Survey Source URL","Metric Type","Tags"] }
CSV.open('/tmp/new_export20190702-4-ng8cew.csv', 'w+', write_parameters) do |new_csv|
CSV.foreach('/tmp/export20190702-4-ng8cew.csv', headers: true) do |row|
puts "#{row['Id']}---#{row['Email']}"
user_email = User.find_by(email: row['Email'])&.effective_email
row['Email'] = user_email || row['Email']
new_csv << row
end
end
网友评论