如何在 Ruby 中创建 CSV 文件某些列的副本,其中一列中有不同的数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12718872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:26:53  来源:igfitidea点击:

How do I create a copy of some columns of a CSV file in Ruby with different data in one column?

rubycsv

提问by user1718712

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”的 CSV 文件。我需要使用来自“A.csv”的数据生成一个名为“B.csv”的新 CSV 文件。

I will be using a subset of columns from "A.csv" and will have to update one column's values to new values in "B.csv". Ultimately, I will use this data from B.csv to validate against a database.

我将使用“A.csv”中的列子集,并且必须将一列的值更新为“B.csv”中的新值。最终,我将使用来自 B.csv 的这些数据对数据库进行验证。

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

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

我是 Ruby 的新手,但我能够读取 CSV 以获取数组或散列。

回答by newUserNameHere

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/rdoc/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", ".00"]
end

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

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

animal,count,price
fox,1,.00


How to append data to a CSV

如何将数据附加到 CSV

Almost the same formula 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 File.open 模式和选项?

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

Now when we open our file.csv we have:

现在,当我们打开我们的 file.csv 时,我们有:

animal,count,price
fox,1,.00
cow,3,2500


Read from our CSV file

从我们的 CSV 文件中读取

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

当然,这就像使用这个 gem 从 CSV 中提取信息的一百种不同方式之一。有关更多信息,我建议您现在访问文档,因为您有一个入门:http: //ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html

回答by MikeB

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.html

回答by Tyler James Young

You will probably want to use CSV::parseto help Ruby understand your CSV as the table of data that it is and enable easy access to values by header.

您可能希望使用它CSV::parse来帮助 Ruby 将您的 CSV 理解为它的数据表,并通过标题轻松访问值。

Unfortunately, the available documentation on the CSV::parsemethoddoesn't make it very clear how to actually use it for this purpose.

不幸的是,关于该CSV::parse方法的可用文档并没有说明如何实际使用它来实现此目的。

I had a similar task and was helped much more by How to Read & Parse CSV Files With Rubyon rubyguides.com than by the CSV class documentation or by the answers pointing to it from here.

我有一个类似的任务,在 ruby​​guides.com 上的How to Read & Parse CSV Files With Ruby比 CSV 类文档或从这里指向它的答案对我的帮助要大得多。

I recommend reading that page in its entirety. The crucial part is about transforming a given CSV into a CSV::Tableobject using:

我建议完整阅读该页面。关键部分是CSV::Table使用以下方法将给定的 CSV 转换为对象:

table = CSV.parse(File.read("cats.csv"), headers: true)

Now there's documentation on the CSV::Tableclass, but again you might be helped more by the clear examples on the rubyguides.com page. One thing I'll highlight is that when you tell .parseto expect headers, the resulting table will treat the first row of data as row [0].

现在有关于CSV::Tableclass文档,但是 ruby​​guides.com 页面上的清晰示例可能会再次为您提供更多帮助。我要强调的一件事是,当您告诉.parse期望标题时,结果表会将第一行数据视为 row [0]

You will probably be especially interested in the .by_colmethod available for your new Tableobject. This will allow you to iterate through different column index positions in the input and/or output and either copy from one to the other or add a new value to the output. If I get it working, I'll come back and post an example.

您可能会对.by_col可用于新Table对象的方法特别感兴趣。这将允许您遍历输入和/或输出中的不同列索引位置,并从一个复制到另一个或向输出添加新值。如果我让它工作,我会回来发布一个例子。