0

这怎么行不通?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's '@y's 并没有修复它。

这是我正在使用的 CSV 示例:

,RegDate,first_Name,last_Name,Email_Address,HomePhone,街道,城市,州,邮政编码

1,11/12/08 10:47,Allison,Nguyen,arannon@jumpstartlab.com,6154385000,3155 19th St NW,Washington,DC,20010

2,11/12/08 13:23,SArah,Hankins,pinalevitsky@jumpstartlab.com,414-520-5000,2022 15th Street NW,Washington,DC,20009

3,11/12/08 13:30,Sarah,Xx,lqrm4462@jumpstartlab.com,(941)979-2000,4175 3rd Street North,Saint Petersburg,FL,33703

4

1 回答 1

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
于 2013-04-18T07:46:28.340 回答