Ruby CSV枚举混淆

时间:2013-04-18 05:09:36

标签: ruby csv

为什么这不起作用? CSV在那里并且有值,我有'要求'csv“和时间在顶部,那里很好。问题似乎是csv.each实际做了什么。

返回

=> [] is the most common registration hour

=> [] is the most common registration day   (Sunday being 0, Mon => 1 ... Sat => 7)

如果我能提供更多信息,请告诉我。

@x = CSV.open \
'event_attendees.csv', headers: true, header_converters: :symbol


def time_target
y = []
@x.each do |line|
    if line[:regdate].to_s.length > 0
        y << DateTime.strptime(line[:regdate], "%m/%d/%y %H:%M").hour
        y = y.sort_by {|i| grep(i).length }.last
    end
end
puts "#{y} is the most common registration hour"
y = []
@x.each do |line|
    if line[:regdate].to_s.length > 0
        y << DateTime.strptime(line[:regdate], "%m/%d/%y %H:%M").wday
        y = y.sort_by {|i| grep(i).length }.last
    end
end
puts "#{y} is the most common registration day \
(Sunday being 0, Mon => 1 ... Sat => 7)"
end

使所有'y''@ y'没有修复它。

以下是我正在使用的CSV中的示例:

  

,RegDate,名字,姓氏,EMAIL_ADDRESS,HOMEPHONE,街道,城市,州,邮政编码

     

1.11 / 12月8日   10:47,Allison,Nguyen,arannon @ jumpstartlab.com,6154385000,3155 19th St   NW,华盛顿,DC,20010

     

2,11 / 12月8日   13:23,莎拉·汉金斯,pinalevitsky @ jumpstartlab.com,414-520-5000,2022   华盛顿特区,15th Street NW,20009

     

3,11 / 12/08 13:30,Sarah,Xx,lqrm4462 @ jumpstartlab.com,(941)979-2000,4175   第三街北,圣彼得堡,佛罗里达州,33703

1 个答案:

答案 0 :(得分:1)

尝试此操作来加载您的数据:

def database_load(arg='event_attendees.csv')
  @contents = CSV.open(arg, headers: true, header_converters: :symbol)
  @people = []
  @contents.each do |row|
    person = {}
    person["id"] = row[0]
    person["regdate"] = row[:regdate]
    person["first_name"] = row[:first_name].downcase.capitalize
    person["last_name"] = row[:last_name].downcase.capitalize
    person["email_address"] = row[:email_address]
    person["homephone"] = PhoneNumber.new(row[:homephone].to_s)
    person["street"] = row[:street]
    person["city"] = City.new(row[:city]).clean
    person["state"] = row[:state]
    person["zipcode"] = Zipcode.new(row[:zipcode]).clean
    @people << person
  end
  puts "Loaded #{@people.count} Records from file: '#{arg}'..."
end