2013-06-12 178 views
2

我已经编写代码,但它不会工作。代码是:如何在Ruby中将字符串保存为CSV文件?

#Parse and print the Tweet if the response code was 200 
tweets = nil 
if response.code == '200' then 
tweets = JSON.parse(response.body) 
require 'csv' 
CSV.open("trial.csv", "ab") do |csv| 
    csv << ["text", "created_at", "name", 'id'] 
    csv << ["tweets"] 
    end 
end 

如何更改此代码以将推文保存到CSV文本文件?

+1

您是否尝试过使用Google搜索? –

+1

我正在认真对抗因为缺乏努力而失败的冲动。该死的,我敢肯定现在有人会这样做,我已经把它放在那里。 –

回答

0

您的代码存在的问题是您正在打印字符串“推文”,而我相信您的意思是打印变量的值tweets。所以需要的变化是删除"tweets"左右的双引号:

CSV.open("trial.csv", "ab") do |csv| 
    csv << ["text", "created_at", "name", 'id'] 
    csv << [tweets] # <- NOTE the change here 
    end 
end 
相关问题