我有一个CSV文件,我需要操纵他们的数据来提取一些结果。 CSV有三种不同类型的数据:name(String),birthDate(Date)和salary(Numeric / float)。我已完成字符串到目前为止的转换,但我不知道如何才能完成这一部分。这是我做的代码:
require 'csv'
class TesteCsv
#Array que recebe os dados do CSV
pessoas = []
#Local do arquivo
file_path = 'C:\\test.csv'
#pre-made converter
CSV::Converters[:my_date] = lambda do |raw_value|
if raw_value =~ /\d{2}\/\d{2}\/\d{4}/
Date.strptime(raw_value, "%d/%m/%Y")
else
raw_value
end
end
pessoas = CSV.read(file_path, :col_sep => ';', :headers => true, :header_converters => :symbol, :converters => :all, :converters => :my_date)
# this is just to show the types of the row data.
puts pessoas[0][:nome].class
puts pessoas[0][:nascimento].class
puts pessoas[0][:salario].class
end
输出是名称[:nome]的字符串,出生日期的日期[:nascimento]和工资字符串[:salario]。 我做错了什么人?