问题描述
我有一个名为A.csv的CSV文件。我需要使用来自A.csv的数据生成一个名为B.csv的新CSV文件。
I have a CSV file called "A.csv". I need to generate a new CSV file called "B.csv" with data from "A.csv".
我将使用A. csv,并且必须在B.csv中将一列的值更新为新值。
I will be using a subset of columns from "A.csv" and will have to update one column's value to new value in "B.csv". Then I use this data from B.csv to validate at database.
- 如何创建新的CSV文件?
- 如何将所需列的数据从A.csv复制到B.csv?
- 如何为特定列添加值?
- How do I create a new CSV file?
- How do I copy the required column's data from A.csv to "B.csv"?
- How do I append values for a particular column?
我可以读取CSV,获取数组或散列。
I am able to read CSV, get an array, or hash.
推荐答案
正如mikeb指出的,有文档 - - 或者,您也可以按照下面的示例(所有的测试和工作):
As mikeb pointed out, there are the docs - http://ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html - Or you can follow along with the examples below (all are tested and working):
在这个文件中,我们将有两行,一个标题行和一个数据行,非常简单的CSV:
In this file we'll have two rows, a header row and data row, very simple CSV:
require "csv"
CSV.open("file.csv", "wb") do |csv|
csv << ["animal", "count", "price"]
csv << ["fox", "1", "$90.00"]
end
文件称为file.csv,具有以下内容:
result, a file called "file.csv" with the following:
animal,count,price
fox,1,$90.00
如何将数据附加到CSV
几乎相同的forumla,而不是使用wb模式,我们将使用a +模式。有关这些的详细信息,请参阅此堆栈溢出答案:
Of course this is like one of like a hundred different ways you can pull info from a CSV using this gem. For more info I suggest visiting the docs now that you have a primer: http://ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html
这篇关于如何在Ruby中创建新的CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!