6

CSVRubyで使用して解析しようとしている「.CSV」ファイルがあります。ただし、ファイルには2行のヘッダーがあり、これまでに遭遇したことがなく、処理方法がわかりません。以下は、ヘッダーと行の例です。

行 1

"Institution ID","Institution","Game Date","Uniform Number","Last Name","First Name","Rushing","","","","","Passing","","","","","","Total Off.","","Receiving","","","Pass Int","","","Fumble Ret","","","Punting","","Punt Ret","","","KO Ret","","","Total TD","Off xpts","","","","Def xpts","","","","FG","","Saf","Points"

行 2

"","","","","","","Rushes","Gain","Loss","Net","TD","Att","Cmp","Int","Yards","TD","Conv","Plays","Yards","No.","Yards","TD","No.","Yards","TD","No.","Yards","TD","No.","Yards","No.","Yards","TD","No.","Yards","TD","","Kicks Att","Kicks Made","R/P Att","R/P Made","Kicks Att","Kicks Made","Int/Fum Att","Int/Fum Made","Att","Made"

行 3

"721","AirForce","09/01/12","19","BASKA","DAVID","","","","","","","","","","","","0","0","","","","","","","","","","2","85","","","","","","","","","","","","","","","","","","","0"

上記の例には戻り値がありません。読みやすいように追加しただけです。この構造を処理するために使用できるメソッドはありますか?CSVそれとも、これを処理するために独自のメソッドを作成する必要がありますか? ありがとう!

4

5 に答える 5

5

gemを使用することをお勧めしsmarter_csv、正しいヘッダーを手動で提供します。

 require 'smarter_csv'
 options = {:user_provided_headers => ["Institution ID","Institution","Game Date","Uniform Number","Last Name","First Name", ... provide all headers here ... ], 
            :headers_in_file => false}
 data = SmarterCSV.process(filename, options)
 data.pop # to ignore the first header line
 data.pop # to ignore the second header line
 # data now contains an array of hashes with your data

オプションと例については、GitHub ページを確認してください。 https://github.com/tilo/smarter_csv

使用する必要があるオプションの 1 つは:user_provided_headersで、必要なヘッダーを配列で指定するだけです。このようにして、このようなケースを回避できます。

data.popファイルのヘッダー行を無視する必要があります。

于 2013-06-06T01:30:25.037 に答える
3

独自のロジックを記述する必要があります。CSV は実際には単なる行と列であり、それ自体には各列または行が実際に何であるかについての固有の考えはありません。それは単なる生データです。したがって、CSV には 2 つのヘッダー行があるという概念や認識がありません。これは人間の仕事であるため、独自のヒューリスティックを構築する必要があります。

データ行が次のようになっているとします。

"721","Air Force","09/01/12",

データの解析を開始するときに、最初の列が整数を表す場合、それを int に変換すると> 0、ヘッダーではなく有効な「行」を扱っていることがわかります。

于 2013-06-06T00:42:58.117 に答える