zoukankan      html  css  js  c++  java
  • 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
    
  • 相关阅读:
    8.1 管道符| 使用技巧
    7.1 elementui的radio无法选中问题
    2.0 es6数组操作
    小练习-双数日期
    匿名函数、三元表达式、列表生成式
    sys.argv
    常用模块 os,sys,
    Python操作数据库
    Python time模块
    加密模块hashlib
  • 原文地址:https://www.cnblogs.com/bluescorpio/p/2980427.html
Copyright © 2011-2022 走看看