• Ruby:Update value on specific row but keep the headers


    昨天在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
    
  • 相关阅读:
    是否完全二叉搜索树 (30 分)
    链表去重
    关于堆的判断
    玩转二叉树
    hdu-2795 Billboard(线段树)
    线段树超级大模版
    博弈dp 以I Love this Game! POJ
    kuangbin 最小生成树
    Infinite Maze CodeForces
    Alice’s Stamps HDU
  • 原文地址:https://www.cnblogs.com/bluescorpio/p/2980427.html
Copyright © 2020-2023  润新知