Car*_*oui 23 ruby csv ruby-on-rails heroku
我希望将我的heroku控制台中的数组导出到本地CSV文件中.
在我目前的情况下,我有一个每日rake任务,寻找推文谈论我的应用程序.我想分析这些推文,看看他们什么时候进来,等等:
heroku run console
tweets = Tweet.all
code to export tweets into a local CSV file goes here
Run Code Online (Sandbox Code Playgroud)
任何想法将不胜感激!
spi*_*ike 33
您无法从heroku控制台访问本地文件系统.一种选择是使用Tee.Tee将输出发送到STDOUT和文件,因此您可以拥有打印所有内容的本地日志.
heroku run console | tee output.txt
Run Code Online (Sandbox Code Playgroud)
小智 20
我按照建议尝试使用Tee,但也陷入困境
Running `console` attached to terminal... up, run.4165
Run Code Online (Sandbox Code Playgroud)
我最终运行了一个SSH shell到localhost,然后通过tee管道.
$ ssh localhost | tee output.txt
$ heroku run console
Run Code Online (Sandbox Code Playgroud)
可能不是最好的解决方案,但它对我有用.
FWIW你可以很容易地用逗号和换行符添加一个字符串,然后将粘贴复制到你的文本编辑器中并保存为.csv,尽管"所有的推文"可能有点笨拙.
tweets = Tweet.all
@string = String.new()
@string << Tweet.last.attributes.keys.join(", ") + "\n" # "header" row with attribute names
tweets.each do |t|
@string << t.attributes.values.join(", ") + "\n"
end
puts @string #will output string with \n newline which you could then copy paste into your editor and save as a csv
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7953 次 |
最近记录: |