我总共有7列,其中有6列最初填写在我正在编写的CSV文件中。当我尝试用数据填充第7列时,我一直遇到这个错误:为什么我无法将值写入空白CSV文件?无错误
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.<<
为什么我一直跑到这个错误?我应该可以将值写入CSV文件的'无'/空白区域。以下是我的代码:
#Finds two records in the database
accounts = Account.find(1,2)
spammer_status = []
#Makes a call into the akismet API and populates spammer_status array with
#true or false values if the person is a spammer or not.
accounts.each do |accounts|
spammer_status << client.comment_check(accounts.last_seen_ip, nil,
:comment_author => accounts.name,
:comment_author_email => accounts.email,
:comment_author_url => accounts.url,
:comment_content => accounts.about)
end
#Changes the values from booleans to strings
spammer_status.map! { |value| value.to_s }
#Populates the initial 6 columns from the database values
CSV.open("/var/local/openhub/current/script/akismet_results.csv","w") do |row|
row << ["id",
"name",
"email",
"url",
"description",
"last seen ip",
"spammer status"]
accounts.each do |accounts|
row << [accounts.id,
accounts.name,
accounts.email,
accounts.url,
accounts.about,
accounts.last_seen_ip]
end
end
#Attempts to populate the 7th column, nil error
CSV.foreach("/var/local/openhub/current/script/akismet_results.csv", headers:true) do |row|
# binding.pry
row[6] << spammer_status.shift
end
我在做什么错在这里?错误在程序的每一部分。我想要做的就是迭代一行,然后将字符串转换后的布尔值添加到正确的列中。任何帮助,将不胜感激?
为什么不能在写第一个6的同时写额外的列? – 2014-09-29 21:02:30
我试过这样做,但因为我的spammer_status是一个数组,它会将所有内容写入第7列。基本上,它将有一个第7列,将[真,假]作为价值。 – 2014-09-29 21:11:20
然后将该值从spammer_status上移开,正如您在第二个循环中尝试 – 2014-09-30 06:17:55