2014-09-29 63 views
0

我总共有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 

我在做什么错在这里?错误在程序的每一部分。我想要做的就是迭代一行,然后将字符串转换后的布尔值添加到正确的列中。任何帮助,将不胜感激?

+0

为什么不能在写第一个6的同时写额外的列? – 2014-09-29 21:02:30

+0

我试过这样做,但因为我的spammer_status是一个数组,它会将所有内容写入第7列。基本上,它将有一个第7列,将[真,假]作为价值。 – 2014-09-29 21:11:20

+0

然后将该值从spammer_status上移开,正如您在第二个循环中尝试 – 2014-09-30 06:17:55

回答

0

您正在尝试将<<设置为nil对象。 row[6]是零。我相信你只是想给row[6]分配一个值,或者如果你想使用<<,只需要row本身。

CSV.foreach("/var/local/openhub/current/script/akismet_results.csv", headers:true) do |row| 
# binding.pry 
    row[6] = spammer_status.shift 
    # or you could do row << spammer_status.shift 
end 
+0

Nobita,感谢您的快速回复。这几乎在那里,但不完全。我的CSV对象的最后一部分看起来像这样:“last seen ip”:“xx.xxx.xxx.xxx”“spammer status”:nil nil:“true”>。最后两个值不会写入csv,因为它们为零。 – 2014-09-29 21:01:14

+0

我不明白你的问题。 – Nobita 2014-09-29 23:28:00

+0

嘿大雄,我想说的是,由于实施,spammer_status的值为零,真正试图写入无列,因此它不写。我终于明白了。谢谢你的帮助。 – 2014-09-30 14:49:44

0

我最终通过重构我的代码来解决这个问题。我不得不承认上述变化写得不好。通过提取垃圾邮件状态作为一种方法,然后在编写CSV时创建一个方法调用,我可以使这些功能正常工作。

def is_spam?(account) 
    spammer_status = client.comment_check(account.last_seen_ip, nil, 
             :comment_author => account.name, 
             :comment_author_email => account.email, 
             :comment_author_url => account.url, 
             :comment_content => account.about) 
    spammer_status.to_s 
end 

CSV.foreach("/var/local/openhub/current/script/akismet_results.csv","a", headers: true) do |row| 
    row << [account.id, 
      account.name, 
      account.email, 
      account.url, 
      account.about, 
      account.last_seen_ip, 
      is_spam?(account)] 
    end 
相关问题