2013-04-18 64 views
0

这是怎么回事? CSV在那里,有价值,我有“需要”csv“和时间在顶部,那里很好。这个问题似乎与csv.each实际上在做任何事情。Ruby CSV枚举混淆

它返回

=> [] 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's'@ y's没有修复它。

这是样品从CSV我使用:

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

1,11/12月8日 10:47,艾里,阮,arannon @ jumpstartlab.com,6154385000,3155 19圣 NW,华盛顿,DC,20010

2,11/12月8日 13:23,萨拉,汉金斯,pinalevitsky @ jumpstartlab.com,414-520-5000,2022 15th Street NW,Washington ,DC,20009

3,11/12月8日13:30,萨拉,XX,lqrm4462 @ jumpstartlab.com,(941)979-2000,4175 三街北,圣彼得堡,佛罗里达州,33703

+0

你需要证明你正在阅读的CSV数据的样本。你的意思是你使用Ruby的CSV gem?如果是这样,请显示使用它的代码。 – 2013-04-18 05:11:26

+0

https://github.com/JumpstartLab/curriculum/blob/master/source/projects/event_attendees.csv – user2251284 2013-04-18 05:13:31

+0

不,将最少量的样本数据嵌入到您的问题中,这样我们就不必追究它。当链路断开时,它会使你的问题难以理解。 – 2013-04-18 05:14:52

回答

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