With regard to the question of python storage and fetch storage, TypeError: and UnicodeDecodeError:

system: win10
IDLE:spyder3.

script: (about saving and fetching storage)

-sharp!/usr/bin/python
-sharp Filename: pickling.py
import cPickle as p
-sharpimport pickle as p
shoplistfile = "shoplist.data"
-sharp the name of the file where we will store the object
shoplist = ["apple", "mango", "carrot"]
-sharp Write to the file
f = file(shoplistfile, "w")
p.dump(shoplist, f) -sharp dump the object to a file
f.close()
del shoplist -sharp remove the shoplist
-sharp Read back from the storage
f = file(shoplistfile)
storedlist = p.load(f)
print storedlist

run TypeError: write () argument must be str, not bytes
(then I found out that f = file (shoplistfile,"w") needs to use "wb", in a two-tier way)

error occurs when running UnicodeDecodeError: "gbk" codec can"t decode byte 0x80 in position 0: illegal multibyte sequence
(prompt storedlist = p.load (f), there is a problem with the sequence here)

there are some reasons that I can"t figure out. I don"t know where it is. How can I solve it?

there is also a question about the software spyder. After running this script once, the script code in spyder disappears for the attribute color, all white, and can not be used to run the function, screenshot.


Mar.30,2022

binary mode is also required to read files

f = file(shoplistfile, 'rb')
Menu