Unicode issue

Solved
Pr.Witherfire Posted messages 84 Status Member -  
mamiemando Posted messages 33228 Registration date   Status Moderator Last intervention   -

Hello

I have a problem with the retrieval of a .yg

 "C:\Program Files\Python311\python.exe" C:\Users\Gwenneg\PycharmProjects\pickle2\main.py    File "C:\Users\Gwenneg\PycharmProjects\pickle2\main.py", line 7     fil=open("C:\Users\Gwenneg\PycharmProjects\pythonProject1\saved.yg","rb")                                                                        ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape Process finished with exit code 1


Thank you


6 answers

Phil_1857 Posted messages 1883 Registration date   Status Member Last intervention   169
 

Hello,

This is not how you use pickle: you don't do pickle.dump(), then open and write to

the file, you dump directly:

 with open('saved.yg','wb') as fo: pickle.dump(saved, fo) 

And for reading:

 with open('saved.yg','rb') as fo: saved = pickle.load(fo)
1
mamiemando Posted messages 33228 Registration date   Status Moderator Last intervention   7 940
 

Hello,

Regarding your response #2, I have never seen the yg extension (pkl seems more natural to me), but why not.

Then, in addition to Phil_1857's response #3, here’s how you can do it.

Code:

import pickle from types import ModuleType def save_all(filename: str): with open(filename, "wb") as f: g = { k : v for (k, v) in globals().items() if ( not (k.startswith("__") and k.endswith("__")) and not callable(v) and not isinstance(v, ModuleType) ) } pickle.dump(g, f) def load_all(filename: str): with open(filename, "rb") as f: for (k, v) in pickle.load(f).items(): globals()[k] = v # Prepare the pickle filename = "toto.pkl" a = 1234567 print(f"saving {filename}") save_all("toto.pkl") # Delete the global variable "a" print("del a") del a try: print(f"a = {a}") except Exception as e: print(e) # Load the pickle print(f"loading {filename}") load_all(filename) print(f"a = {a}") 

Result

 saving toto.pkl del a name 'a' is not defined loading toto.pkl a = 1234567

Good luck

1
mamiemando Posted messages 33228 Registration date   Status Moderator Last intervention   7 940
 

Hello,

The question is too vague.

  1. What is a yg file?
  2. How did you create it?
  3. How are you trying to open it?

Good luck

0
Pr.Witherfire Posted messages 84 Status Member
 

Hello

1. It is a binary file extension

2. I did it using the following code:

import pickle def sauvonstout(): globs = list(globals()) saved = [] for v in globs: d = globals()[v] t = (str(type(d))) if t != "<class 'module'>" and t !="<class 'function'>": saved.append((v,pickle.dumps(d))) bytessaved = pickle.dumps(saved) fil = open("saved.yg","wb") fil.write(bytessaved) fil.close() a=1234567 sauvonstout()

3. Using the following code:

import pickle def restore(pb): b = pickle.loads(pb) for n, p in b: v = pickle.loads(p) globals()["yg"+n] = pickle.loads(p) fil = open("saved.yg","rb") pb = fil.read() restore(pb) print(yga)
0
Pr.Witherfire Posted messages 84 Status Member
 

Ok thanks. For the yg file, I haven't seen it either. I based my code on a response from yg_be. I wonder if it's a format he created... Thank you


0
mamiemando Posted messages 33228 Registration date   Status Moderator Last intervention   7 940
 

Haha, it might be a way to sign his work, we should ask him ;-) But ".pkl" seems more telling to me ;-)

0
yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588 > mamiemando Posted messages 33228 Registration date   Status Moderator Last intervention  
 

Out of inspiration, I took the first name that came to mind.

0
mamiemando Posted messages 33228 Registration date   Status Moderator Last intervention   7 940 > yg_be Posted messages 23437 Registration date   Status Contributor Last intervention  
 

The funniest part is that when I saw the extension, I thought: "yg, yg... mmmh apart from yg_be it doesn't ring a bell at all..." :D

0
Pr.Witherfire Posted messages 84 Status Member > mamiemando Posted messages 33228 Registration date   Status Moderator Last intervention  
 

;)

0
Pr.Witherfire Posted messages 84 Status Member > yg_be Posted messages 23437 Registration date   Status Contributor Last intervention  
 

Cool. I'm not familiar with this, but does that mean you can create your own format anytime?!

0
Pr.Witherfire Posted messages 84 Status Member
 

Yes, still, it seemed to be working... :)


0