我已经编写了代码,但它不起作用。代码是:
#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 文本文件?
最佳答案
您的代码的问题在于您正在打印字符串“tweets”,而我认为您打算打印变量 tweets
的值。所以需要的更改是删除 "tweets"
周围的双引号:
CSV.open("trial.csv", "ab") do |csv|
csv << ["text", "created_at", "name", 'id']
csv << [tweets] # <- NOTE the change here
end
end
关于ruby - 如何在 Ruby 中将字符串保存到 CSV 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17068904/