重新启动程序后追加列表时删除 pickle 数据

pickle data is deleted when list is appended to after restarting program

每次我重新启动我的程序时,它都会显示以前附加到和腌制的列表。当我尝试向列表中添加新数据时出现问题。以前存储的数据被删除,新数据被腌制。我真的很困惑,并且一直在尝试解决这个问题 2 天。任何帮助都会很棒,谢谢!

我的项目背景: 我正在尝试制作一个 gui,使我能够轻松保存各种类型的数据,以便以后可以使用该数据进行可视化表示。

这是我的整个程序:

import pickle
import os
os.system("clear")
import tkinter as tk

win = tk.Tk()
win.title("first gui")
#Program
#Functions
tips = []
days = []
amount_of_cars = []
weather = []
temperature = []



def add_all_stuff():
    pickle1 = open("tips.txt","wb")
    pickle2 = open("days.txt","wb")
    pickle3 = open("cars.txt","wb")
    pickle4 = open("weather.txt","wb")
    pickle5 = open("temperature.txt","wb")
    a1=tips_ask.get()
    a2=days_ask.get()
    a3=cars_ask.get()
    a4=weather_ask.get()
    a5=temperature_ask.get()
    print(temperature_ask.get())
    print(weather_ask.get())
    print(cars_ask.get())
    print(days_ask.get())
    print(tips_ask.get())
    tips.append(a1)
    days.append(a2)
    amount_of_cars.append(a3)
    weather.append(a4)
    temperature.append(a5)
    print(tips)
    print(days)
    print(amount_of_cars)
    print(weather)
    print(temperature)
    #pickle dumps
    pickle.dump(tips, pickle1)
    pickle1.close

    pickle.dump(days, pickle2)
    pickle2.close

    pickle.dump(tips, pickle3)
    pickle3.close

    pickle.dump(tips, pickle4)
    pickle4.close

    pickle.dump(tips, pickle5)
    pickle5.close

def show_lists():
    pickle_in1 = open("tips.txt", "rb")
    tips = pickle.load(pickle_in1)
    print(tips)

#labels
tk.Label(win, text = "menu").grid(row=0)
tk.Label(win, text= "Tips").grid(row=1)
tk.Label(win, text= "Day").grid(row=2)
tk.Label(win, text= "Amount of Cars").grid(row=3)
tk.Label(win, text= "Weather").grid(row=4)
tk.Label(win, text= "Temperature").grid(row=5)
#entryboxes
tips_ask = tk.Entry(win)
tips_ask.grid(row=1, column=1)

days_ask = tk.Entry(win)
days_ask.grid(row=2, column=1)

cars_ask = tk.Entry(win)
cars_ask.grid(row=3, column=1)

weather_ask = tk.Entry(win)
weather_ask.grid(row=4, column=1)

temperature_ask = tk.Entry(win)
temperature_ask.grid(row=5, column=1)
#Buttons
tk.Button(win, text="add", command = add_all_stuff).grid(row=6, column = 1)
tk.Button(win, text="View saved lists", command = show_lists).grid(row=6, column=2)
win.mainloop()

您需要先读取 pickle 数据。您正在打开文件进行写入,而不是在此之前将数据读取到您附加到的变量中。打开 'wb' 会覆盖文件。所以

    pickle1 = open("tips.txt","wb")

删除并打开一个空文件进行写入。

您可以添加

def add_all_stuff():
    tips = open("tips.txt","rb")
    days = open("days.txt","rb")
    amount_of_cars = open("cars.txt","rb")
    weather = open("weather.txt","rb")
    temperature = open("temperature.txt","rb")
    #... the rest of your code, that should help

更新:这是一个完整的程序,即使在关闭后也能正常工作:

import pickle
import os
os.system("clear")
import tkinter as tk

win = tk.Tk()
win.title("first gui")
#Program
#Functions
tips = []
days = []
amount_of_cars = []
weather = []
temperature = []


def get_make_pickle_data(filename):
   if os.path.isfile(filename):
       pickle_data = pickle.load( open( filename, "rb" ) )
   else:
       pickle_data = []
       pickle.dump( pickle_data, open( filename, "wb" ) )
   return pickle_data


def add_all_stuff():

    tips=get_make_pickle_data('tips.txt')
    days=get_make_pickle_data('days.txt')
    amount_of_cars=get_make_pickle_data('amount_of_cars.txt')
    weather=get_make_pickle_data('weather.txt')
    temperature=get_make_pickle_data('temperature.txt')

    a1=tips_ask.get()
    a2=days_ask.get()
    a3=cars_ask.get()
    a4=weather_ask.get()
    a5=temperature_ask.get()
    print(temperature_ask.get())
    print(weather_ask.get())
    print(cars_ask.get())
    print(days_ask.get())
    print(tips_ask.get())
    tips.append(a1)
    days.append(a2)
    amount_of_cars.append(a3)
    weather.append(a4)
    temperature.append(a5)
    print(tips)
    print(days)
    print(amount_of_cars)
    print(weather)
    print(temperature)
    #pickle dumps


    pickle1 = open("tips.txt","wb")
    pickle2 = open("days.txt","wb")
    pickle3 = open("amount_of_cars.txt","wb")
    pickle4 = open("weather.txt","wb")
    pickle5 = open("temperature.txt","wb")


    pickle.dump(tips, pickle1)
    pickle1.close

    pickle.dump(days, pickle2)
    pickle2.close

    pickle.dump(amount_of_cars, pickle3)
    pickle3.close

    pickle.dump(weather, pickle4)
    pickle4.close

    pickle.dump(temperature, pickle5)
    pickle5.close

def show_lists():
    tips=get_make_pickle_data('tips.txt')
    days=get_make_pickle_data('days.txt')
    amount_of_cars=get_make_pickle_data('amount_of_cars.txt')
    weather=get_make_pickle_data('weather.txt')
    temperature=get_make_pickle_data('temperature.txt')
    print(tips,days,amount_of_cars,weather,temperature)

#labels
tk.Label(win, text = "menu").grid(row=0)
tk.Label(win, text= "Tips").grid(row=1)
tk.Label(win, text= "Day").grid(row=2)
tk.Label(win, text= "Amount of Cars").grid(row=3)
tk.Label(win, text= "Weather").grid(row=4)
tk.Label(win, text= "Temperature").grid(row=5)
#entryboxes
tips_ask = tk.Entry(win)
tips_ask.grid(row=1, column=1)

days_ask = tk.Entry(win)
days_ask.grid(row=2, column=1)

cars_ask = tk.Entry(win)
cars_ask.grid(row=3, column=1)

weather_ask = tk.Entry(win)
weather_ask.grid(row=4, column=1)

temperature_ask = tk.Entry(win)
temperature_ask.grid(row=5, column=1)
#Buttons
tk.Button(win, text="add", command = add_all_stuff).grid(row=6, column = 1)
tk.Button(win, text="View saved lists", command = show_lists).grid(row=6, column=2)
win.mainloop()