last day (14 days later) » 

8:06 AM
0
Q: Using pickle, not opening? Python

Angus RyanI am trying to save my board (Connect 4 game) at any time using pickle. Although when I try to open the file nothing happens. The file is saved on my desktop although nothing opens? Here is the save code and the open code: Save Code: fp = 'C://Users/Angus/Desktop/file.pkl' with open(fp, 'wb') a...

 
what are escape characters?
 
What happens if you run: print(type(board)) after opening the pickled board?
 
it says <class 'list'>
when I print(type(board))
 
Ok, so assuming that your board was stored as a list (which would make sense!) it is successfully being unpickled. So back to the question others have asked..... what isn't happening that you'd expect to happen? How would you expect it to open?
Ah hang on, are you testing the type of the "board" that you pickle, or the "board" that is returned after you unpickle it?
 
basically, I'm using pygame, and I have a connect 4 board, where people click a button and it places counters in the coordinates necessary e.g. when button 5 clicked it gets placed in the 5th column, at row 6 (bottom of grid) which is in my code coordinate board[5][5]
and I asked in a previous question on stack overflow, if I wanted to give players the ability mid game to save the counters that have been placed on the grid, how would I do it? and I got told to pickle it and the unpickle it when wanting it to be open
so basically when I open the save I was hoping for all the counters last saved to be in their postions
 
8:06 AM
So you are being returned an empty list? (If you check, it should evaluate as False if this is the case), or just print it out to look at it.
Not 100% sure that answers your question, but if you are pickling a list, it is mutable, so this might be the problem, but I don't understand why it should be! Try converting your board to a tuple first, which is immutable, pickle it, then upickle it and see if it comes back with something. (you can then convert it back to a list)
 
Can I ask you a couple more in depth questions, if you have time?
 
8:31 AM
Happy to try and help but I'm still confused as to what is being returned. Are you getting a blank list?
 
9:15 AM
Yeah, the board coordinates have no been stored
so when I open the pickled file
its just an empty grid
if you have pygame, i could send you the code for you to see for yourself?
 
 
3 hours later…
12:24 PM
Sorry for the delay in responding - at work.

First, could you try converting the list to a tuple or set?

l = [1,2,3,4]
tuple(l)

Then try pickling it and see what is returned?
 

last day (14 days later) »