美文网首页工作生活
Rails 大CSV文件字段修改

Rails 大CSV文件字段修改

作者: ifree321 | 来源:发表于2019-07-03 13:51 被阅读0次

    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
    

    相关文章

      网友评论

        本文标题:Rails 大CSV文件字段修改

        本文链接:https://www.haomeiwen.com/subject/zyuqhctx.html