从哈希创建CSV(Creating a CSV from a hash)

我需要帮助了解如何将我的哈希内容转储到csv中。 我发现以下帖子正是我正在尝试做的,但不幸的是,没有关于如何调用该方法以便它将写入文件的示例。

原始帖子在红宝石中巧妙地将哈希数组转换为CSV

def to_csv(csv_filename="hash.csv") require 'csv' # Get all unique keys into an array: keys = self.flat_map(&:keys).uniq CSV.open(csv_filename, "wb") do |csv| csv << keys self.each do |hash| # fetch values at keys location, inserting null if not found. csv << hash.values_at(*keys) end end end

我的哈希数组称为结果,看起来像这样:(注意:某些键有nil值)

[{:name =>"Mike", :job = "Teacher", :status = "single"}, {:name=> "Joe", :job =>nil, :status = "married"}, {:name =>"Sally", :job = "Nurse", :status = "single"}]

我想将结果哈希的全部内容转储到CSV文件中以便进一步操作。 我的问题是我无法弄清楚如何将结果导入csv文件。 当我跑:

results.to_csv

没有任何反应,没有创建文件。 我想我可能不得不首先播种一个空白文件,但即使这样,也没有内容进入该文件。

有人可以帮我理解我做错了什么以及为什么这不起作用。 在此先感谢您的任何帮助。

I need help understanding how to dump my hash contents into a csv. I found the following post that is exactly what I'm trying to do, but unfortunately there is no example on how to call the method so that it will write to a file.

original post Smartly converting array of hashes to CSV in ruby

def to_csv(csv_filename="hash.csv") require 'csv' # Get all unique keys into an array: keys = self.flat_map(&:keys).uniq CSV.open(csv_filename, "wb") do |csv| csv << keys self.each do |hash| # fetch values at keys location, inserting null if not found. csv << hash.values_at(*keys) end end end

My array of hashes is called results and looks something like this: (Note: some keys have nil values)

[{:name =>"Mike", :job = "Teacher", :status = "single"}, {:name=> "Joe", :job =>nil, :status = "married"}, {:name =>"Sally", :job = "Nurse", :status = "single"}]

I would like to dump the entire contents of the results hash into a CSV file for further manipulation. My problem is I can't figure out how to get the results into csv file. When I run:

results.to_csv

nothing happens, no file is created. I thought I may have to first seed a blank file, but even then, no contents go into the file.

Can someone please help me understand what I'm doing wrong and why this isn't working. Thanks in advance for any assistance.

最满意答案

您需要将此方法定义为类Array的实例方法。

class Array def to_csv(csv_filename="hash.csv") require 'csv' # Get all unique keys into an array: keys = self.flat_map(&:keys).uniq CSV.open(csv_filename, "wb") do |csv| csv << keys self.each do |hash| # fetch values at keys location, inserting null if not found. csv << hash.values_at(*keys) end end end end [{ :a=>1, :b=>2 }, { :c=>3 }].to_csv #=> [{:a=>1, :b=>2}, {:c=>3}]

让我们看看写的是什么。

CSV.read("hash.csv") #=> [["a", "b", "c"], ["1", "2", nil], [nil, nil, "3"]]

我推断这是一个来自代码片段的数组方法

self.flat_map(&:keys)

由于映射是键,它表明reiceiver是一个哈希数组。(该表达式可以写成flat_map(&:keys) 。)

此方法似乎不符合您的要求。

您可以这样保存键和值:

CSV.open("hash.csv", "wb") do |csv| arr.each do |h| csv << h end end #=> [["a", "b", "c"], [1, 2, 3], [4, 5, 6]] CSV.read("hash.csv") #=> [["a", "b", "c"], [1, 2, 3], [4, 5, 6]]

You need to define this method as an instance method of the class Array.

class Array def to_csv(csv_filename="hash.csv") require 'csv' # Get all unique keys into an array: keys = self.flat_map(&:keys).uniq CSV.open(csv_filename, "wb") do |csv| csv << keys self.each do |hash| # fetch values at keys location, inserting null if not found. csv << hash.values_at(*keys) end end end end [{ :a=>1, :b=>2 }, { :c=>3 }].to_csv #=> [{:a=>1, :b=>2}, {:c=>3}]

Let's see what was written.

CSV.read("hash.csv") #=> [["a", "b", "c"], ["1", "2", nil], [nil, nil, "3"]]

I infer this is an array method from the code fragment

self.flat_map(&:keys)

As the mapping is to keys, it suggests that the reiceiver is an array of hashes.(That expression could have been written flat_map(&:keys).)

This method does not appear to meet your requirements.

You could save the keys and values thusly:

CSV.open("hash.csv", "wb") do |csv| arr.each do |h| csv << h end end #=> [["a", "b", "c"], [1, 2, 3], [4, 5, 6]] CSV.read("hash.csv") #=> [["a", "b", "c"], [1, 2, 3], [4, 5, 6]]从哈希创建CSV(Creating a CSV from a hash)

我需要帮助了解如何将我的哈希内容转储到csv中。 我发现以下帖子正是我正在尝试做的,但不幸的是,没有关于如何调用该方法以便它将写入文件的示例。

原始帖子在红宝石中巧妙地将哈希数组转换为CSV

def to_csv(csv_filename="hash.csv") require 'csv' # Get all unique keys into an array: keys = self.flat_map(&:keys).uniq CSV.open(csv_filename, "wb") do |csv| csv << keys self.each do |hash| # fetch values at keys location, inserting null if not found. csv << hash.values_at(*keys) end end end

我的哈希数组称为结果,看起来像这样:(注意:某些键有nil值)

[{:name =>"Mike", :job = "Teacher", :status = "single"}, {:name=> "Joe", :job =>nil, :status = "married"}, {:name =>"Sally", :job = "Nurse", :status = "single"}]

我想将结果哈希的全部内容转储到CSV文件中以便进一步操作。 我的问题是我无法弄清楚如何将结果导入csv文件。 当我跑:

results.to_csv

没有任何反应,没有创建文件。 我想我可能不得不首先播种一个空白文件,但即使这样,也没有内容进入该文件。

有人可以帮我理解我做错了什么以及为什么这不起作用。 在此先感谢您的任何帮助。

I need help understanding how to dump my hash contents into a csv. I found the following post that is exactly what I'm trying to do, but unfortunately there is no example on how to call the method so that it will write to a file.

original post Smartly converting array of hashes to CSV in ruby

def to_csv(csv_filename="hash.csv") require 'csv' # Get all unique keys into an array: keys = self.flat_map(&:keys).uniq CSV.open(csv_filename, "wb") do |csv| csv << keys self.each do |hash| # fetch values at keys location, inserting null if not found. csv << hash.values_at(*keys) end end end

My array of hashes is called results and looks something like this: (Note: some keys have nil values)

[{:name =>"Mike", :job = "Teacher", :status = "single"}, {:name=> "Joe", :job =>nil, :status = "married"}, {:name =>"Sally", :job = "Nurse", :status = "single"}]

I would like to dump the entire contents of the results hash into a CSV file for further manipulation. My problem is I can't figure out how to get the results into csv file. When I run:

results.to_csv

nothing happens, no file is created. I thought I may have to first seed a blank file, but even then, no contents go into the file.

Can someone please help me understand what I'm doing wrong and why this isn't working. Thanks in advance for any assistance.

最满意答案

您需要将此方法定义为类Array的实例方法。

class Array def to_csv(csv_filename="hash.csv") require 'csv' # Get all unique keys into an array: keys = self.flat_map(&:keys).uniq CSV.open(csv_filename, "wb") do |csv| csv << keys self.each do |hash| # fetch values at keys location, inserting null if not found. csv << hash.values_at(*keys) end end end end [{ :a=>1, :b=>2 }, { :c=>3 }].to_csv #=> [{:a=>1, :b=>2}, {:c=>3}]

让我们看看写的是什么。

CSV.read("hash.csv") #=> [["a", "b", "c"], ["1", "2", nil], [nil, nil, "3"]]

我推断这是一个来自代码片段的数组方法

self.flat_map(&:keys)

由于映射是键,它表明reiceiver是一个哈希数组。(该表达式可以写成flat_map(&:keys) 。)

此方法似乎不符合您的要求。

您可以这样保存键和值:

CSV.open("hash.csv", "wb") do |csv| arr.each do |h| csv << h end end #=> [["a", "b", "c"], [1, 2, 3], [4, 5, 6]] CSV.read("hash.csv") #=> [["a", "b", "c"], [1, 2, 3], [4, 5, 6]]

You need to define this method as an instance method of the class Array.

class Array def to_csv(csv_filename="hash.csv") require 'csv' # Get all unique keys into an array: keys = self.flat_map(&:keys).uniq CSV.open(csv_filename, "wb") do |csv| csv << keys self.each do |hash| # fetch values at keys location, inserting null if not found. csv << hash.values_at(*keys) end end end end [{ :a=>1, :b=>2 }, { :c=>3 }].to_csv #=> [{:a=>1, :b=>2}, {:c=>3}]

Let's see what was written.

CSV.read("hash.csv") #=> [["a", "b", "c"], ["1", "2", nil], [nil, nil, "3"]]

I infer this is an array method from the code fragment

self.flat_map(&:keys)

As the mapping is to keys, it suggests that the reiceiver is an array of hashes.(That expression could have been written flat_map(&:keys).)

This method does not appear to meet your requirements.

You could save the keys and values thusly:

CSV.open("hash.csv", "wb") do |csv| arr.each do |h| csv << h end end #=> [["a", "b", "c"], [1, 2, 3], [4, 5, 6]] CSV.read("hash.csv") #=> [["a", "b", "c"], [1, 2, 3], [4, 5, 6]]