我有一个嵌套哈希,如下所示:
{
"0001" => {
"All nodes" => [N001, N002, N003],
"All links" => [N001.1, N002.1, N003.1],
"Pumps" => [N001.2]
},
"0002" => {
"All nodes" => [N004, N005, N006],
"All links" => [N004.1, N005.1, N006.1],
"Pumps" => [N005.2]
},
"0003" => {
"All nodes" => [N007, N008, N009],
"All links" => [N007.1, N008.1, N009.1],
"Pumps" => [N007.2]
}
}
节点下存储坐标等信息,链接下存储反演、直径和存储,泵下存储开/关水位和流量。
我想知道,如果你有任何想法,如何导出到CSV的信息存储在散列,但在正确的列(这将是散列键(0001,0002和0003)).例如,这是我设法到现在:
require 'CSV'
net=WSApplication.current_network
CSVsaveloc=WSApplication.file_dialog(false, "csv", "Comma Separated Variable File", "testexportcsv",false,true)
f = File.new(CSVsaveloc, "w")
CSV.open(CSVsaveloc,"wb") do |csv|
csv << Hash.keys
Hash.each do |key,values|
csv << x.mean
csv << y.mean
csv << diameter.min
csv << discharge.min
end
end
现在我得到的导出如下:
0001,0002,0003
246164.2646
518466.7589
300mm
0.01
246181.6492
518444.1727
250mm
0.005
246171.5763
518509.8948
500mm
0.1
但是,我希望它是这样的:
0001,0002,0003
246164.2646,246181.6492,246171.5763
518466.7589,518444.1727,518509.8948
300mm,250mm,500mm
0.01,0.005,0.1
1条答案
按热度按时间kt06eoxx1#
据我所知,似乎每次运行
csv <<
时CSV都会插入一个新行,为每一行创建一个数组,然后从那里追加到CSV中可能会有所帮助。从您前面给出的示例代码扩展,也许您可以尝试这样做?