Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pickle- how does it work
#1
I made a simple text fighting game.but i need to be able to save and load variables.i was tols to use pickle. But i have no experience in what it even is.i googled arouns but i can't understand how it works
Basicly i have some variables
And need to save them to a file.then load them

name=str(jonathan)
Damage=int(10)
# and so on and ao forth.these variablea change depending on level.i need to be able to save them to a file which is named (name).so here it will be called jonathan.later read these variables and set health and such accordingly to continue playing.simple things as im really new
Please help
Thanks
Reply
#2
Don't use pickle,use Json is't just better and you get human readable file on disk.
Data serialization is the concept of converting structured data into a format that allows it to be shared,
or stored in such a way that its original structure to be recovered.

Instead of storing stuff in singe variables think of a data structure like eg dictionary.
>>> record = dict(name='jonathan', Damage=10)
>>> record
{'Damage': 10, 'name': 'jonathan'} 
import json

record = dict(name='jonathan', Damage=10)
with open("my_file.json", "w") as j:
   json.dump(record, j)
with open("my_file.json") as j_out:
   saved_date = json.load(j_out)

print(saved_date) #--> {'Damage': 10, 'name': 'jonathan'}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 413 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  pickle problem DPaul 13 4,838 Sep-27-2022, 05:25 PM
Last Post: DPaul
  TypeError: cannot pickle n00sferatu 1 2,662 Dec-14-2021, 03:52 PM
Last Post: yakkaligiri
  Multiprocessing Can't pickle local object law 1 16,060 Aug-30-2021, 02:49 PM
Last Post: law
  Save/Loading using pickle Scordomaniac 4 3,054 Nov-24-2020, 06:11 PM
Last Post: Scordomaniac
  computing entropy using pickle files baran01 2 2,442 Dec-30-2019, 09:45 PM
Last Post: micseydel
  Tkinter don't get ver from file via pickle storzo 2 2,576 Jul-31-2019, 03:50 PM
Last Post: storzo
  pickle docs say bytes in one place, strings in another Skaperen 2 2,160 Jul-29-2019, 05:13 PM
Last Post: Skaperen
  pickle error SheeppOSU 4 10,993 Apr-20-2019, 04:50 PM
Last Post: SheeppOSU
  Using pickle.dump Friend 1 2,955 Feb-15-2019, 04:39 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020