我想在Ruby on Rails中使用自定义格式将哈希转换为csv。
输入哈希:
[{"Date"=>"2019-03-18", "Date Hour"=>"2019-03-18 00:00", "Price"=>"14"}, {"Date"=>"2019-03-18", "Date Hour"=>"2019-03-18 01:00", "Price"=>"13"}, {"Date"=>"2019-03-18", "Date Hour"=>"2019-03-18 02:00", "Price"=>"14"}, {"Date"=>"2019-03-18", "Date Hour"=>"2019-03-18 03:00", "Price"=>"19"}, {"Date"=>"2019-03-18", "Date Hour"=>"2019-03-18 04:00", "Price"=>"28"}, {"Date"=>"2019-03-18", "Date Hour"=>"2019-03-18 05:00", "Price"=>"24"}, {"Date"=>"2019-03-18", "Date Hour"=>"2019-03-18 06:00", "Price"=>"21"},
{"Date"=>"2019-03-19", "Date Hour"=>"2019-03-19 00:00", "Price"=>"16"}, {"Date"=>"2019-03-19", "Date Hour"=>"2019-03-19 01:00", "Price"=>"10"}, {"Date"=>"2019-03-19", "Date Hour"=>"2019-03-19 02:00", "Price"=>"11"}, {"Date"=>"2019-03-19", "Date Hour"=>"2019-03-19 03:00", "Price"=>"14"}, {"Date"=>"2019-03-19", "Date Hour"=>"2019-03-19 04:00", "Price"=>"26"}, {"Date"=>"2019-03-19", "Date Hour"=>"2019-03-19 05:00", "Price"=>"22"},
{"Date"=>"2019-03-19", "Date Hour"=>"2019-03-19 06:00", "Price"=>"25"}
预期产出:
Date,Date Hour,Mar-18,Mar-19
2019-01-01,00:00,14,16
2019-01-01,01:00,13,10
2019-01-01,02:00,14,11
2019-01-01,03:00,19,14
2019-01-01,04:00,28,26
2019-01-01,05:00,24,22
2019-01-01,06:00,21,25
目前输入哈希有2个日期,它可能包含多个日期值。基于上面的输入,我想构建具有上述预期输出的csv?
如何获得上述预期输出?
更新:这是我的代码。但我的代码仅适用于单日数据。如果输入csv中有多天的数据,我怎样才能增强它?
# Convert csv to hash
rows = CSV.parse(response, headers: true).map(&:to_h)
# Add transformation in hash
rows.each do |hash|
# Remove Hour from Date Hour values
hash["Date Hour"] = hash["Date Hour"].gsub!(hash["Date"], "").strip
# Replace Price column name with date value (e.g Mar-19)
hash[Date.parse(hash["Date"]).strftime("%b-%d")] = hash.delete("Price")
# Set static Date
hash["Date"] = "2019-01-01"
end
# Convert hash to csv
# Extract column names from first row of data
column_names = rows.first.keys
# Generate CSV after transformation of csv
csv_response = CSV.generate do |csv|
csv << column_names
rows.each do |row|
# Extract values for row of data
csv << row.values_at(*column_names)
end
end
首先改变你的输入:
result =
input.group_by do |hash|
hash["Date Hour"].split.last
end.values.map do |arr|
arr.each_with_object({}) do |h, acc|
acc["Date"] ||= "2019-01-01"
acc["Date Hour"] ||= h["Date Hour"].split.last
acc[Date.iso8601(h["Date"]).strftime('%b-%d')] = h["Price"]
end
end
#⇒ [{"Date"=>"2019-01-01", "Date Hour"=>"00:00", "Mar-18"=>"14", "Mar-19"=>"16"},
# {"Date"=>"2019-01-01", "Date Hour"=>"01:00", "Mar-18"=>"13", "Mar-19"=>"10"},
# {"Date"=>"2019-01-01", "Date Hour"=>"02:00", "Mar-18"=>"14", "Mar-19"=>"11"},
# {"Date"=>"2019-01-01", "Date Hour"=>"03:00", "Mar-18"=>"19", "Mar-19"=>"14"},
# {"Date"=>"2019-01-01", "Date Hour"=>"04:00", "Mar-18"=>"28", "Mar-19"=>"26"},
# {"Date"=>"2019-01-01", "Date Hour"=>"05:00", "Mar-18"=>"24", "Mar-19"=>"22"},
# {"Date"=>"2019-01-01", "Date Hour"=>"06:00", "Mar-18"=>"21", "Mar-19"=>"25"}]
现在只需吐出CSV。
无耻的插件:可以使用SeeAsVee
gem从哈希数组中生成CSV文件:
require 'see_as_vee'
SeeAsVee.csv result
#⇒ #<File:/tmp/am/see_as_vee20190320-10403-ub0p5y.csv>