使用 pickle load 获取一行数据并显示在 table 中?

Get a row of data and display in table using pickle load?

我是 Python 的新手,正在学习。我使用 pickle dump 将 class 对象存储到使用 wb 的文本文件中,效果很好。我现在需要根据用户输入的日期从文件中获取特定的数据行。我如何使用 pickle.load 执行此操作?这将需要显示在table中。

class LotteryDraw():

    def __init__(self,date,ball_1,ball_2,ball_3,ball_4,ball_5,ball_6,bonus_ball):

        self.date = date
        self.ball_1 = ball_1
        self.ball_2 = ball_2
        self.ball_3= ball_3
        self.ball_4 = ball_4
        self.ball_4 = ball_4
        self.ball_5 = ball_5
        self.ball_5 = ball_5
        self.ball_6 = ball_6
        self.bonus_ball = bonus_ball
def add_data(LotteryDraw):

    lottery_list = []

    date= valid_date() # calls valid_date() gets date and checks in valid format
    ball_1 = input('\tEnter number for ball 1: ') # gets ball 1 input then ball 2 etc
    ball_2 = input('\tEnter number for ball 2: ')
    ball_3 = input('\tEnter number for ball 3: ')
    ball_4 = input('\tEnter number for ball 4: ')
    ball_5 = input('\tEnter number for ball 5: ')
    ball_6 = input('\tEnter number for ball 6: ')
    bonus_ball = input('\tEnter bonus ball number: ')
    lottery_list.append(LotteryDraw(date,ball_1,ball_2,ball_3,ball_4,ball_5,ball_6,bonus_ball))

    save(lottery_list)

    return lottery_list
def save(lottery_list):

    with open("lottery_draw2.txt", mode="wb") as filename:
        pickle.dump(lottery_list, filename)
    print("Data saved to file.")
    print()

    return filename

我尽力回答你了。 The code:

import pickle
import os.path

class LotteryDraw():
    def __init__(self,date,ball_1,ball_2,ball_3,ball_4,ball_5,ball_6,bonus_ball):
        self.date = date
        self.ball_1 = ball_1
        self.ball_2 = ball_2
        self.ball_3 = ball_3
        self.ball_4 = ball_4
        self.ball_4 = ball_4
        self.ball_5 = ball_5
        self.ball_5 = ball_5
        self.ball_6 = ball_6
        self.bonus_ball = bonus_ball

    def print_details(self):
        print('object values: ',self.ball_1, self.ball_2, self.ball_3, self.ball_4, self.ball_5, self.ball_6, self.bonus_ball)

def valid_date(date):
    # do some validity check
    is_valid = True # hardcoded to true
    return is_valid, date

def add_data():
    if(os.path.isfile("lottery_draw2.p")):
        lottery_list = pickle.load(open( "lottery_draw2.p", "rb" ))
    else:
        # that means no such file name exists, we need to create empty list
        lottery_list = []

    is_valid, date= valid_date(input('Enter date: ')) # calls valid_date() gets date and checks in valid format

    while(not is_valid):
        # loop until user gives correct format
        print('please input correct date')
        is_valid, date= valid_date(input('Enter date: '))

    ball_1 = input('\tEnter number for ball 1: ') # gets ball 1 input then ball 2 etc
    ball_2 = input('\tEnter number for ball 2: ')
    ball_3 = input('\tEnter number for ball 3: ')
    ball_4 = input('\tEnter number for ball 4: ')
    ball_5 = input('\tEnter number for ball 5: ')
    ball_6 = input('\tEnter number for ball 6: ')
    bonus_ball = input('\tEnter bonus ball number: ')
    lottery_list.append(LotteryDraw(date,ball_1,ball_2,ball_3,ball_4,ball_5,ball_6,bonus_ball))

    save(lottery_list)
    return lottery_list

def save(lottery_list):
    pickle.dump(lottery_list, open( "lottery_draw2.p", "wb" ) )
    print("Data saved to file.")


if __name__ == '__main__':
    add_data() # data entry 1
    add_data() # data entry 2
    add_data() # data entry 3

    # opening the pickle file
    lottery_list = pickle.load(open( "lottery_draw2.p", "rb" ))
    searching_date = '08-05-2020'
    for each_plyer in lottery_list:
        if(each_plyer.date == searching_date):
            each_plyer.print_details()

输入与输出

Enter date: 08-05-2020
    Enter number for ball 1: 1
    Enter number for ball 2: 1
    Enter number for ball 3: 1
    Enter number for ball 4: 1
    Enter number for ball 5: 1
    Enter number for ball 6: 1
    Enter bonus ball number: 1
Data saved to file.
Enter date: 07-05-2020
    Enter number for ball 1: 2
    Enter number for ball 2: 2
    Enter number for ball 3: 2
    Enter number for ball 4: 2
    Enter number for ball 5: 2
    Enter number for ball 6: 2
    Enter bonus ball number: 2
Data saved to file.
Enter date: 08-05-2020
    Enter number for ball 1: 7
    Enter number for ball 2: 7
    Enter number for ball 3: 7
    Enter number for ball 4: 7
    Enter number for ball 5: 7
    Enter number for ball 6: 7
    Enter bonus ball number: 7
Data saved to file.
object values:  1 1 1 1 1 1 1
object values:  7 7 7 7 7 7 7