Ruby将csv解析为可用的哈希值

时间:2016-10-05 20:53:34

标签: ruby

我正在制作NBA球队非农就业数据。我有这个csv:http://www.basketball-reference.com/contracts/

Rk,Team,2016-17,2017-18,2018-19,2019-20,2020-21,2021-22
1,Cleveland Cavaliers,$123590274,$118585590,$111958508,$65464580,,
2,Los Angeles Clippers,$118663837,$111415942,$59741545,$2500725,,
3,Portland Trail Blazers,$115817639,$138409348,$121862764,$118747593,$62965110,
4,Dallas Mavericks,$112488859,$94965622,$62766454,$35361887,,
5,Memphis Grizzlies,$114096737,$97742075,$88773581,$86212960,$34504132,
6,San Antonio Spurs,$110631827,$96744068,$56909494,$25990334,,
7,Detroit Pistons,$110492645,$103423474,$97903566,$62944822,$28751775,
8,Orlando Magic,$110283846,$74591158,$60217493,$43122365,$17000000,
9,Toronto Raptors,$109253824,$102105184,$84967199,$51464677,$27739975,
10,Washington Wizards,$107257619,$99510391,$103018516,$49454077,$28751775,
11,Miami Heat,$105296376,$95803765,$94126541,$65558388,,
12,Golden State Warriors,$104677735,$66269011,$40907537,$20844187,,
13,New York Knicks,$104501658,$77290233,$78325386,$41195895,,
14,Milwaukee Bucks,$103338094,$112928334,$89209363,$76367783,$29393637,$1865547
15,Los Angeles Lakers,$102354756,$84416158,$62677311,$56232985,,
16,New Orleans Pelicans,$102177578,$78434268,$67966414,$65355463,$28751775,
17,Charlotte Hornets,$101879187,$91900885,$74203714,$53571467,$27130434,
18,Atlanta Hawks,$101793777,$70277529,$46071092,$25355630,,
19,Sacramento Kings,$99168232,$84890581,$27219195,$8350732,,
20,Houston Rockets,$97639130,$97370875,$73164132,$68025858,,
21,Chicago Bulls,$96908430,$81438211,$42663896,$24345313,,
22,Oklahoma City Thunder,$96181060,$68581249,$68960837,$8863055,,
23,Boston Celtics,$95289212,$77610987,$50413474,$45792877,,
24,Indiana Pacers,$91950761,$80519787,$62913923,,,
25,Minnesota Timberwolves,$84638527,$68085957,$37620814,$5348007,,
26,Utah Jazz,$84386693,$71138358,$17001288,,,
27,Phoenix Suns,$84297090,$71123131,$66028579,$26744625,,
28,Denver Nuggets,$79627212,$81852764,$49452791,$10497490,,
29,Brooklyn Nets,$78769729,$60910873,$25603813,$9344638,,
30,Philadelphia 76ers,$75336267,$50155264,$26385506,$14125600,,

在红宝石中我写道:

def payrolls

payrolls = {}

CSV.foreach("payrolls.csv", :headers => true, :header_converters => :symbol, :converters => :all) do |row|
  payrolls[row.fields[1]] = Hash[row.headers[1..-1].zip(row.fields[1..-1])]
end

 puts payrolls.inspect

end

哪个输出:

{    
"Cleveland Cavaliers"=>{:team=>"Cleveland Cavaliers", :"201617"=>"$123590274", :"201718"=>"$118585590", :"201819"=>"$111958508", :"201920"=>"$65464580", :"202021"=>nil, :"202122"=>nil}, 
"Los Angeles Clippers"=>{:team=>"Los Angeles Clippers", :"201617"=>"$118663837", :"201718"=>"$111415942", :"201819"=>"$59741545", :"201920"=>"$2500725", :"202021"=>nil, :"202122"=>nil
}

哪个相当实用。但是,由于年份标题是一个数字,当我使用

payrolls[Cleveland Cavaliers][:201617]

我收到了这个错误:

payrolls.rb:31: syntax error, unexpected tINTEGER, expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END
  puts payrolls["Cleveland Cavaliers"][:201617]

因此,获取图表薪资数据的最佳方法是什么?

2 个答案:

答案 0 :(得分:0)

使用字符串而不是符号:

payrolls["Cleveland Cavaliers"]["201617"]

答案 1 :(得分:0)

  1. 您的代码有一个语法错误我已修复:您没有倒数第二个}

    hash = {    
    "Cleveland Cavaliers"=>{:team=>"Cleveland Cavaliers", :"201617"=>"$123590274", :"201718"=>"$118585590", :"201819"=>"$111958508", :"201920"=>"$65464580", :"202021"=>nil, :"202122"=>nil}, 
    "Los Angeles Clippers"=>{:team=>"Los Angeles Clippers", :"201617"=>"$118663837", :"201718"=>"$111415942", :"201819"=>"$59741545", :"201920"=>"$2500725", :"202021"=>nil, :"202122"=>nil}
    }
    
  2. 您应该通过密钥访问哈希。 Cleveland Cavaliers:201617都不是有效对象,因此您会收到错误。

  3. 您正在关注的是:"201617"

    hash["Cleveland Cavaliers"][:"201617"]
    #=> "$123590274"