【问题标题】:How to save save data and reuse it after rerunning the program? [duplicate]重新运行程序后如何保存保存数据并重复使用? [复制]
【发布时间】:2019-11-05 16:19:01
【问题描述】:

我使用以下代码获取股票价格数据:

for i in range(25200):

    time.sleep(1)
    with requests.Session() as s:
               data = {'ContractCode' : 'SAFMO98' }
               r = s.post('http://cdn.ime.co.ir/Services/Fut_Live_Loc_Service.asmx/GetContractInfo', json = data ).json()

    for key, value in r.items():
        plt.clf()
        last_prices = (r[key]['LastTradedPrice'])   
        z.append(last_prices)
        plt.figure(1)
        plt.plot(z)

有时我的程序会断开连接或停止。然后我必须重新运行程序,我丢失了所有数据,程序从头开始。我正在寻找一种方法来保存数据并在重新运行程序后重用它。怎么可能?

我应该在我的代码中添加什么来做到这一点?

编辑:我编辑了我的代码,如下所示,但两种方法都不适合我:

try:
    with open('3_tir.pickle', 'rb') as f:    
           last_prices = pickle.load(f)
           print("pickle loaded")
   #f = open("last_prices.txt", 'a+')
   #f.read()


except Exception:
    #f = open("last_prices.txt", 'a+')
    pass

for i in range(25200):

    time.sleep(1)
    with requests.Session() as s:
               data = {'ContractCode' : 'SAFMO98' }
               r = s.post('http://cdn.ime.co.ir/Services/Fut_Live_Loc_Service.asmx/GetContractInfo', json = data ).json()

    for key, value in r.items():
        plt.clf()
        last_prices = (r[key]['LastTradedPrice'])   
        z.append(last_prices)
        plt.figure(1)
        plt.plot(z)

    with open('3_tir.pickle', 'wb') as f:
           pickle.dump(last_prices, f)
    #   f.write(last_prices)
    #   f.close()

【问题讨论】:

  • @不在 python 中!
  • @martineau:对不起。我想在其他问题中写下该评论。

标签: python save dataset storage


【解决方案1】:

您可以使用 pickle 将列表、元组、类等对象存储到文件中,并在程序重新启动时将它们加载回内存中。它的工作方式类似于 json 库。

使用pickle.dump() 保存对象,pickle.load() 将其加载回内存。

演示:保存到 PICKLE 文件

import pickle

a_list = [2,3,4,5]

with open('pickled_list.pickle', 'wb') as f:
    pickle.dump(a_list, f)

演示:从 PICKLE 文件加载

import pickle

with open('pickled_list.pickle', 'rb') as f:    
    list_from_pickle = pickle.load(f)

print(list_from_pickle)

输出:

[2,3,4,5]

访问 Python 软件基金会的此页面,详细了解可以腌制的内容以及您还可以做什么:https://docs.python.org/3/library/pickle.html

【讨论】:

  • 谢谢,但我如何在加载泡菜时添加新数据?
  • 从pickle加载的数据成为原始对象。也许你应该单独问这个问题,以便我们可以给出更详细的答案,以帮助其他人。
  • 我编辑了我的问题,请阅读编辑部分,谢谢。
  • 你是什么意思?您的意思是向之前的泡菜添加新数据需要一个新问题吗?
  • 我希望您提出一个新问题,围绕您在代码当前阶段的独特体验,因为这已被标记为重复。目前,其他人无法贡献,因为这被标记为重复。 :)
【解决方案2】:

您可以将数据写入文件:

f = open("file.txt", 'w+')
f.write(last_prices)
# write your program here
f.close()

或附加它:

f = open("file.txt", 'a+')
f.write(last_prices)
# write your program here
f.close()

然后你可以从那个文件中读取。

f = open("file.txt")

您可以通过 .read() 方法访问整个文本,也可以通过 .readlines() 方法逐行获取文本。

f.read()
# It returns the whole text in one string
f.readlines()
# It returns a list of the file's lines

More information about reading and writing to a file. 如果您可以将数据添加到数据库表之类的东西中,那么您也可以使用CSV Files 来保存您的数据。 You can use CSV Library.

编辑:我不知道您要做什么,但显然您没有加载文件。我可以看到您从“3_tir.pickle”加载,但您从未使用过它!您将文件加载到“last_prices”变量,然后在 20 行后重新分配(再次定义该变量)它。所以我建议你阅读this article,然后阅读this,这样你就可以更好地编写程序了。

【讨论】:

  • 不行!!!
  • 我编辑了我的问题,请阅读编辑部分,谢谢。
猜你喜欢
  • 2017-05-08
  • 2021-05-03
  • 1970-01-01
  • 1970-01-01
  • 2015-04-29
  • 2020-05-18
  • 1970-01-01
  • 2016-09-08
  • 1970-01-01
相关资源
最近更新 更多