2

I'm currently programming a simple pygame platformer, and I'm using Python's pickle module. For each of the levels I have different files. I'm trying to store them in the world_data variable with this code:

if path.exists(f'level{level}_data'):
    pickle_in = open(f'level{level}_data', 'rb')
    world_data = pickle.load(pickle_in)
world = World(world_data)

but it gives me this error:

line 333, in <module>
    world_data = pickle.load(pickle_in)
_pickle.UnpicklingError: invalid load key, '['.

I searched everywhere but couldn't find an answer.

This is what one of my level files look like:

[
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 1],
[1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 2, 2, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 7, 0, 5, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 1],
[1, 7, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 7, 0, 0, 0, 0, 1],
[1, 0, 2, 0, 0, 7, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 2, 0, 0, 4, 0, 0, 0, 3, 0, 0, 3, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 7, 0, 0, 0, 0, 2, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 2, 2, 2, 2, 2, 1],
[1, 0, 0, 0, 0, 0, 2, 2, 2, 6, 6, 6, 6, 6, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]

Do I got something wrong with my file? Or something wrong with my code? Please help me

  • share the code where you do `pickle.dump` – drum Dec 31 '20 at 16:19
  • 1
    `pickle` is a compressed binary format. You can only `load` something that has been `pickle.dump`ed. If your level file looks exactly like your example, that is not a valid pickle file. (Though you could probably load it with `json.load` or `ast.literal_eval`.) – 0x5453 Dec 31 '20 at 16:25
  • I don't exactly do a pickle.dump, so I will try to change my level's format and then try to run it again. But in which format does it need to be? – Abdullah Bera Koksal Dec 31 '20 at 16:28
  • You can use your level-files as they are. See my answer. – wuerfelfreak Dec 31 '20 at 16:30

1 Answers1

1

As you can read in the pickle-Docs, pickle is for serilization. You can't use it to read in your level-files.

To read you level-files you can use something like proposed in this answer by Roger Pate.

>>> import ast
>>> x = '[ "A","B","C" , " D"]'
>>> x = ast.literal_eval(x)
>>> x
['A', 'B', 'C', ' D']

in your code it would look like

if path.exists(f'level{level}_data'):
    ast_in = open(f'level{level}_data', 'rb')
    world_data = ast.literal_eval(ast_in.read())
world = World(world_data)
wuerfelfreak
  • 2,363
  • 1
  • 14
  • 29