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

前端 未结 6 1300
萌比男神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:43

    To get the best of both worlds (very fast reading from a huge file AND the benefits of a native Ruby CSV object) my code had since evolved into this method:

    $stock="XTEX"
    csv_data = CSV.parse IO.read(%`|sed -n "1p; /^#{$stock},/p" stocks.csv`), {:headers => true, :return_headers => false, :header_converters => :symbol, :converters => :all}
    
    # Now the 1-row CSV object is ready for use, eg:
    $company = csv_data[:company][0]
    $volatility_month = csv_data[:volatility_month][0].to_f
    $sector = csv_data[:sector][0]
    $industry = csv_data[:industry][0]
    $rsi14d = csv_data[:relative_strength_index_14][0].to_f
    

    which is closer to my original method, but only reads in one record plus line 1 of the input csv file containing the headers. The inline sed instructions take care of that--and the whole thing is noticably instant. This this is better than last because now I can access all the fields from Ruby, and associatively, not caring about column numbers anymore as was the case with awk.

提交回复
热议问题