【发布时间】: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