【问题标题】:What's the best way to save historical data?保存历史数据的最佳方法是什么?
【发布时间】:2018-06-16 11:31:45
【问题描述】:

我️想用 Python 编写一个程序来保存我️做的加密货币购买/销售的历史。 I️ 希望能够保存购买时间、交易时的货币价格和利润等数据以寻找模式。我️将如何保存这些数据?

【问题讨论】:

  • 您是在询问如何在 python 中创建/写入文件吗?你有什么具体问题?
  • ...到目前为止你做了什么代码/研究?
  • Saving an Object (Data persistence) 问题的答案可能会有所帮助。

标签: python save


【解决方案1】:

您可以很容易地让自己成为一个编写历史文件或记录为纯 .txt 文件的程序:

import datetime
import os

os.chdir("C:\Users\<username>\Documents") #Location of history file

day_of_purchase = datetime.date.today()
price_of_currency = 10
profit = 0

file_object  = open("textfile.txt", "w")
file_object.write(str(day_of_purchase)+"," +str(price_of_currency)+"," +str(profit))
file_object.close()

如果您想继续将历史输出添加到文件中,只需附加 .txt 文件即可。您还可以使用 csv 模块设置更复杂的 .csv 文件:https://docs.python.org/3.4/library/csv.html。您还可以使用一些更详细的库来保存 excel 文件,尽管 excel 非常能够直接读取 .csv 文件,使用指定字符作为列分隔符。 (在上述情况下,选择逗号作为分隔符。)

您可以通过创建一些交互式组件(例如 input() 参数)来使程序更加复杂。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    • 2015-07-13
    相关资源
    最近更新 更多