0

如何解析以下格式的大型 csv 文件:

A,1,X:1,X:1,X:1,X:1...

我需要保存A和所有1的。我想将1's 保存为逗号分隔:

A 1,1,1,1,1,1...

数据示例:

4217,23,4217:0.1304,11045842:0.0870,11027563:0.0435,15055960:0.0435,12556773:0.0435,10317812:0.0435,21268053:0.0435,14982717:0.0435,12560416:0.0435,21684075:0.0435,12177392:0.0435,878710:0.0435,21777845:0.0435,11045966:0.0435,17109375:0.0435,15701596:0.0435,10312162:0.0435,11045878:0.0435

我的期望:

4217 4217,11045842,11027563,15055960,12556773,10317812,21268053,14982717,12560416,21684075,12177392,878710,21777845,11045966,17109375,15701596,10312162,11045878

4

1 回答 1

2

假设您将输入作为变量中的字符串,例如row

#Filename: test.rb

row = "4217,23,4217:0.1304,11045842:0.0870,11027563:0.0435,15055960:0.0435,12556773:0.0435,10317812:0.0435,21268053:0.0435,14982717:0.0435,12560416:0.0435,21684075:0.0435,12177392:0.0435,878710:0.0435,21777845:0.0435,11045966:0.0435,17109375:0.0435,15701596:0.0435,10312162:0.0435,11045878:0.0435";

row=row.split(',').map do |x|
        if(x.index(':')!=nil)
            x[0..(x.index(':')-1)]
        else
            x
        end
    end

keyElement = row[0];
arrayElement = row[2..-1];

puts keyElement;
for i in 0..arrayElement.length-1
    print(arrayElement[i] + ", ");
end
puts arrayElement[-1];

这里keyElement将包含“A”并将arrayElement逗号分隔值作为数组包含。

对于您的示例数据,它给出:

>>ruby test.rb
4217
4217, 11045842, 11027563, 15055960, 12556773, 10317812, 21268053, 14982717, 12560416, 21684075, 12177392, 878710, 21777845, 11045966, 17109375, 15701596, 10312162, 11045878, 11045878

但是,我会推荐一个哈希,假设您需要将数组与值“A”相关联:

#row is mapped as before

hashElement = Hash.new();
hashElement[row[0]] = row[2..-1];
于 2013-03-05T18:00:43.067 回答