2013-04-18 96 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