这是我的csv文件值
Name , Sales
Randy, 200
Robin, 502
Randy, 200
Raj, 502
Randy, 500
Robin, 102
Mano, 220
Raj, 502
Randy, 285
Robin, 385
Randy, 295
Raj, 596
我需要这个输出
Name Sales Rank
Randy 1596 1
Raj 1354 2
Robin 988 3
Mano 860 4
当我使用 max and most
它只显示最大值,最大值对我不起作用。我需要帮助请任何人帮我谢谢
cust = Hash.new
CSV.foreach(("./test.csv"), headers: true, col_sep: ",") do |row|
cust.store(row["Name"],row["Sales"])
end
print cust
我目前正在尝试使用哈希,但它会保存最后的数据
2条答案
按热度按时间bf1o4zei1#
如果要使用
row["COL"]
csv语法。此外,您还需要使用+=
不=
把它们加起来。现在,您只是每次都覆盖该值。生产:
sgtfey8w2#
首先,让我们创建一个包含该信息的文件。
csv文件具有固定的列分隔符(字符串)。按照惯例,分隔符将是逗号,在这种情况下,该文件的内容将显示如下:
或者,也可以使用分离器
", "
,但这不是所有行的分隔符。第一行在逗号前包含一个空格,其他一些行在逗号后包含两个或更多空格。因此,您应该使用逗号(默认值)作为列分隔符,然后在需要时,去掉开头和结尾的空格。我们可以先打开文件来创建一个
CSV
例如。我们会发现
csv.class #=> CSV
及csv.headers #=> true
. 后者只是确认我们已经规定文件有头。由于尚未读取文件中的任何内容,因此不会返回头本身。读取标题后的第一行后csv.headers
将返回一个标题数组。你不需要col_sep: ","
因为默认的列分隔符是逗号。现在我们读取文件并计算感兴趣的散列。
作为
CSV.included_modules.include?(Enumerable) #=> true
,可枚举#每个带有_对象的_枚举csv
,它们是csv_行的示例(块变量的值csv_row
)并以hash::new方法的形式创建一个计数哈希,该方法接受一个参数(默认值)而不接受任何块。请注意,在计算中name #=> "Name "
及sales #=> " Sales"
.我们现在可以检索标题:
最好关闭该文件:
虽然方法
CSV#close
没有记录。我们现在可以操纵
h
及headers
符合要求。例如,我们可以计算以下各项:然后以良好的格式显示结果。
这将显示以下内容。
请参阅string#ljust和string#rjust。也可以使用字符串#%或内核#sprintf。
CSV
具有内置转换器(和标头转换器)。如果有人写道:文件正文中的所有值都将转换为整数。不幸的是,这将转换
name
字段转换为整数以及sales
这不是我们想要的。但是,我们可以创建一个自定义转换器来仅进行转换sales
将值转换为整数。这是这样做的。然后我们可能会改变
csv_row[sales].to_i
到csv_row[sales]
:如果有多个自定义转换器,我们会写:
哪里
proc1
,proc2
,... 是实现自定义转换器的过程。如果只有一个自定义转换器,如这里所示,我们可以编写converters: proc
或converters: [proc]
.最后,如果您对使用csv文件有点生疏,您可以简单地将该文件视为普通文件:
因此:
然后像以前一样继续。