0

我正在使用 JumpStart Labs 事件管理器,特别是一周中的时间/日期定位,我遇到了麻烦。当我通过终端运行以下代码时,它给了我以下错误[编辑]:

austin-winslows-macbook-4:event_manager HypnoBoy$ ruby event_manager.rb
event_manager.rb:8: odd number list for Hash
...vent_attendees.csv', {headers: true, header_converters: :sym...
                          ^
event_manager.rb:8: syntax error, unexpected ':', expecting '}'
...vent_attendees.csv', {headers: true, header_converters: :sym...
                          ^
event_manager.rb:8: Can't assign to true
...ttendees.csv', {headers: true, header_converters: :symbol})
                          ^
event_manager.rb:8: syntax error, unexpected ':', expecting '='
...ders: true, header_converters: :symbol})

我已经在下面发布了我的代码,正在寻找建议!关于语法的某些东西显然是错误的,但到目前为止我已经按照字母的步骤进行操作,并且没有遇到任何问题,所以我不知道该去哪里找了。任何帮助将是一个很大的帮助,谢谢!

require 'csv'
require 'sunlight/congress'
require 'erb'
require 'date'


Sunglight::Congress.api_key = "e179a6973728c4dd3fb1204283aaccb5"
contents = CSV.open('event_attendees.csv', {headers: true, header_converters: :symbol})


def clean_zipcode(zipcode)
zipcode.to_s.rjust(5,"0")[0..4]
end


def clean_phone(number)
number.to_s.rjust(10,"0")[0..4] 
end


def legislators_by_zipcode(zipcode)
Sunglight::Congress::Legislator.by_zipcode(zipcode)
end


def peak_days
time = row[:regdate]
day_array = []
time.each { |t|
    array << Datetime.strptime(t, '%m/%d/%Y %H:%M').wday }
end

def peak_hours
time = row[:regdate]
hr_array = []
time.each { |t|
        array << DateTime.strptime(t, '%m/%d/%Y %H:%M').hour }
array
end


def save_thanks_you_letters(id,form_letter)
Dir.mkdir("output") unless Dir.exists? "output"

filename = "output/thanks_#{id}.html"

File.open(filename, 'w') { |file|
    file.puts form_letter} 
end


puts "EventManager Initialized!"

template_letter = File.read "form_letter.erb"
erb_template = ERB.new template_letter

contents.each { |row|
id = row[0]

name = row[:first_name]

zipcode = clean_zipcode(row[:zipcode])

phone = clean_phone(row[:homephone]) 

legislators = legislators_by_zipcode(zipcode)

form_letter = erb_template.result(binding)

save_thank_you_letters(id,form_letter)
}
4

2 回答 2

0

CSV::open从您正在使用该构造的文档中:

open( filename, options = Hash.new )

所以你行:

contents = CSV.open 'event_attendees.csv', headers: true, header_converters: :symbol是错误的,因为从第二个参数开始它需要一个Hash. 因此将其更改为:

contents = CSV.open('event_attendees.csv', {headers: true, header_converters: :symbol})
于 2013-05-01T20:05:41.573 回答
0

我今天完成了这个练习。我不必更改 contents = CSV.open 行。对我来说导致错误的是日期未在 Excel 文件中格式化。我在 Excel 中将该日期列格式化为 mm/dd/yyyy hh:mm。在 '%m/%d/%Y %H:%M' 字符串中,大写似乎也很重要——我使用了小写的 'y'。

这是我第一次练习的样子:

# Iteration: Time Targeting
contents = CSV.open "event_attendees.csv", headers: true, header_converters: :symbol

regtimes = Array.new(25, 0)

contents.each do |row|

  reghour = DateTime.strptime(row[:regdate],'%m/%d/%y %H:%M').hour

  regtimes[reghour] += 1

end
于 2013-05-03T04:34:23.837 回答