Why my code doesn't save/load my data in pytho

2019-08-01 02:29发布

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.

0条回答
登录 后发表回答