import from CSV into Ruby array, with 1st field as hash key, then lookup a field's value given header row

前端 未结 6 1284
萌比男神i
萌比男神i 2021-01-31 04:17

Maybe somebody can help me.

Starting with a CSV file like so:

Ticker,\"Price\",\"Market Cap\"
ZUMZ,30.00,933.90
XTEX,16.02,811.57
AAC,9.83,80.02
         


        
6条回答
  •  时光说笑
    2021-01-31 04:41

    CSV.read(file_path, headers:true, header_converters: :symbol, converters: :all).collect do |row|
      Hash[row.collect { |c,r| [c,r] }]
    end
    

提交回复
热议问题