我使用以下代码(您可以复制并运行该代码,它可以工作)加载(如果存在)并保存数据以在我的程序的下一次执行中重复使用,但是它不起作用:>
import matplotlib.pyplot as plt
import time
import requests
import pickle
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 = {'current' : 'none' }
r = s.get('http://call5.tgju.org/ajax.json?2019061716-20190617171520-I4OJ3OcWf4gtpzr3JNC5' , json = data ).json()
plt.clf()
price = r['current']['diff_aud_usd']['p']
z.append(price)
plt.figure(1)
plt.plot(z)
plt.pause(0.1)
with open('3_tir.pickle', 'wb') as f:
pickle.dump(last_prices, f)
# f.write(last_prices)
# f.close()
我尝试同时使用pickle和file方式,但是它们都不起作用!我没有任何错误,但是数据将不会保存,程序只会绘制新数据。