如何在Ruby中创建新的CSV文件?

时间:2022-05-15 06:39:12

I have a CSV file called "A.csv". I need to generate a new CSV file called "B.csv" with data from "A.csv".

我有一个CSV文件叫做" a。CSV "我需要生成一个新的CSV文件,名为“B”。csv"和来自"A.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.

我将使用“a”中的一组列。并且必须将一列的值更新为“B.csv”中的新值。然后我用B的数据。在数据库中验证的csv。

  1. How do I create a new CSV file?
  2. 如何创建新的CSV文件?
  3. How do I copy the required column's data from A.csv to "B.csv"?
  4. 如何从A复制所需列的数据。csv“B.csv”?
  5. How do I append values for a particular column?
  6. 如何为特定的列追加值?

I am able to read CSV, get an array, or hash.

我可以读取CSV,获取数组或散列。

2 个解决方案

#1


137  

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):

正如mikeb所指出的,有文档——http://ruby-doc.org/stdlib-1.9.3/ libdoc/csv/r/csv.html——或者你可以跟着下面的例子(都是经过测试和工作的):

To create a new file:

In this file we'll have two rows, a header row and data row, very simple CSV:

在这个文件中我们有两行,头行和数据行,非常简单的CSV:

require "csv"
CSV.open("file.csv", "wb") do |csv|
  csv << ["animal", "count", "price"]
  csv << ["fox", "1", "$90.00"]
end

result, a file called "file.csv" with the following:

结果,一个名为“file”的文件。csv”如下:

animal,count,price
fox,1,$90.00

How to append data to a CSV

Almost the same forumla as above only instead of using "wb" mode, we'll use "a+" mode. For more information on these see this stack overflow answer: What are the Ruby File.open modes and options?

几乎和上面一样,我们使用a+模式而不是wb模式。有关这些问题的更多信息,请参见这个堆栈溢出回答:什么是Ruby文件。开放的模式和选择?

CSV.open("file.csv", "a+") do |csv|
  csv << ["cow", "3","2500"]
end

Now when we open our file.csv we have:

现在打开文件。csv我们有:

animal,count,price
fox,1,$90.00
cow,3,2500

Read from our CSV file

Now you know how to copy and to write to a file, to read a CSV and therefore grab the data for manipulation you just do:

现在你知道了如何复制和写入文件,读取CSV,并因此获取数据进行操作,你只需要:

CSV.foreach("file.csv") do |row|
  puts row #first row would be ["animal", "count", "price"] - etc.
end

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

当然,这是你从CSV中获取信息的上百种方式之一。如果想了解更多信息,我建议您现在就访问文档:http://ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html

#2


4  

Have you seen Ruby's CSV class? It seems pretty comprehensive. Check it out here: http://ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html

你看过Ruby的CSV类吗?很全面。请在这里查看:http://ruby-doc.org/stdlib-1.9.3/ libdoc/csv/rdoc/csv/csv/csv/csv/csv/csv/csv/csv/csv/csv/csv.html。

#1


137  

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):

正如mikeb所指出的,有文档——http://ruby-doc.org/stdlib-1.9.3/ libdoc/csv/r/csv.html——或者你可以跟着下面的例子(都是经过测试和工作的):

To create a new file:

In this file we'll have two rows, a header row and data row, very simple CSV:

在这个文件中我们有两行,头行和数据行,非常简单的CSV:

require "csv"
CSV.open("file.csv", "wb") do |csv|
  csv << ["animal", "count", "price"]
  csv << ["fox", "1", "$90.00"]
end

result, a file called "file.csv" with the following:

结果,一个名为“file”的文件。csv”如下:

animal,count,price
fox,1,$90.00

How to append data to a CSV

Almost the same forumla as above only instead of using "wb" mode, we'll use "a+" mode. For more information on these see this stack overflow answer: What are the Ruby File.open modes and options?

几乎和上面一样,我们使用a+模式而不是wb模式。有关这些问题的更多信息,请参见这个堆栈溢出回答:什么是Ruby文件。开放的模式和选择?

CSV.open("file.csv", "a+") do |csv|
  csv << ["cow", "3","2500"]
end

Now when we open our file.csv we have:

现在打开文件。csv我们有:

animal,count,price
fox,1,$90.00
cow,3,2500

Read from our CSV file

Now you know how to copy and to write to a file, to read a CSV and therefore grab the data for manipulation you just do:

现在你知道了如何复制和写入文件,读取CSV,并因此获取数据进行操作,你只需要:

CSV.foreach("file.csv") do |row|
  puts row #first row would be ["animal", "count", "price"] - etc.
end

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

当然,这是你从CSV中获取信息的上百种方式之一。如果想了解更多信息,我建议您现在就访问文档:http://ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html

#2


4  

Have you seen Ruby's CSV class? It seems pretty comprehensive. Check it out here: http://ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html

你看过Ruby的CSV类吗?很全面。请在这里查看:http://ruby-doc.org/stdlib-1.9.3/ libdoc/csv/rdoc/csv/csv/csv/csv/csv/csv/csv/csv/csv/csv/csv.html。