获取一行数据并使用pickle加载在表中显示



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

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

相关内容

  • 没有找到相关文章

最新更新