解析CSV文件

时间:2013-03-05 17:51:52

标签: ruby-on-rails ruby

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

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

我需要保存A和所有1。我想以逗号分隔保存1

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

1 个答案:

答案 0 :(得分: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];