Why my code doesn't save/load my data in python?

自闭症网瘾萝莉.ら 提交于 2019-12-13 17:25:25

问题


I use the following code(you can copy and run the code, it works) to load(if exists) and save the data to reuse it in the next execution of my program, but it doesn't work:

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()

I tried to use both pickle and file way but none of them worked! I get no errors but data won't be saved and program plots only the new data.

来源:https://stackoverflow.com/questions/56737479/why-my-code-doesnt-save-load-my-data-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!