我有一个试图在ruby中使用CSV
解析的“.CSV”文件。该文件有两行标题,但我以前从未遇到过,也不知道如何处理它。下面是标题和行的示例。
第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
是否有可用的方法来处理此结构,还是我必须编写自己的方法来处理此结构?谢谢! 最佳答案
您必须编写自己的逻辑。 CSV实际上只是行和列,其本身并不了解每个列或行的真正含义,它只是原始数据。因此,CSV没有任何概念或意识,因为它有两个标题行,这是很自然的事情,因此您需要构建自己的启发式方法。
假设您的数据行如下所示:
"721","Air Force","09/01/12",
当您开始解析数据时,如果第一列表示整数,则将其转换为int且它的
> 0
超出了您所知道的,您正在处理的是有效的“行”而不是 header 。