Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Im not sure how to operate a pickle file. I am trying to populate data to my .pkl file and I do not know if

Im not sure how to operate a pickle file. I am trying to populate data to my .pkl file and I do not know if I write data to it manually or have to write it through my pickle_demo.py file. Can you instruct me on how to get data from pickle to .pkl. Currently my players.pkl file is empty.

pickle_demo.py

import os

import os.path

import pickle

import locale

from blackjackgame.player import Player

def main():

"""Load or create player data and save it to a file."""

players = "name name name name name".split()

num_players = None

main_dir = os.path.dirname(os.path.abspath(__file__))

data_dir = os.path.join(main_dir, "data")

pickle_file = os.path.join(data_dir, "players.pkl")

locale.setlocale(locale.LC_ALL, "en_US.utf8")

if os.path.exists(pickle_file):

with open(pickle_file, "rb") as file_handle:

players = pickle.load(file_handle)

num_players = len(players)

else:

while num_players is None or num_players > 4 or num_players < 1:

num_players = int(input("How many players (1-4)? "))

if num_players > 4 or num_players < 1:

print("Please select a number between 1 and 4")

print(f"Let's make {num_players} players...")

players = [

Player(

name=input(f"Enter name for player {i + 1}: "),

player_id=i + 1,

)

for i in range(num_players)

]

print("We made the following players:")

for player in players:

print(f"{player.name} has {locale.currency(player.bankroll)}")

print("Let's save the players to a file...")

if not os.path.exists(data_dir):

os.mkdir(data_dir)

with open(pickle_file, "wb") as file_handle:

pickle.dump(players, file_handle, pickle.HIGHEST_PROTOCOL)

if __name__ == "__main__":

main()

players.pkl

// file empty. I do not know what to write

// upvote

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

6th Edition

1943153035, 978-1943153039

Students also viewed these Databases questions

Question

What are Measures in OLAP Cubes?

Answered: 1 week ago

Question

How do OLAP Databases provide for Drilling Down into data?

Answered: 1 week ago

Question

How are OLAP Cubes different from Production Relational Databases?

Answered: 1 week ago