Python - Error when trying to add comments -
i learning reading/writing strings, lists, etc txt/dat files. wanted add comment in code can refer access keys are. did.
# mode description # rb read binary file. if file doesn’t exist, python complain error. # wb write binary file. if file exists, contents overwritten. if file doesn’t exist, # it’s created. # ab append binary file. if file exists, new data appended it. if file doesn’t exist, it’s # created. # rb+ read , write binary file. if file doesn’t exist, python complain # error. # wb+ write , read binary file. if file exists, contents overwritten. if file # doesn’t exist, it’s created. # ab+ append , read binary file. and after have:
import pickle, shelve print("pickling lists.") variety = ["sweet", "hot", "dill"] shape = ["whole", "spear", "chip"] brand = ["claussen", "heinz", "vlassic"] f = open("pickles1.dat", "wb") pickle.dump(variety, f) pickle.dump(shape, f) pickle.dump(brand, f) f.close() print("\nunpickling lists.") f = open("pickles1.dat", "rb") variety = pickle.load(f) shape = pickle.load(f) brand = pickle.load(f) print(variety) print(shape) print(brand) f.close() when run following error:
syntaxerror: non-utf-8 code starting '\x92' in file pickleit.py on line 10, no encoding declared; see http://python.org/dev/peps/pep-0263/ details
i checked out link, don't understand it, haven't seen before.
oh , apologies line 10 # rb
replace ’ '. it's complaining because didn't add encoding type beginning of file. default encoding utf-8, in characters not allowed.
instead of replacing, can add line beginning (before comments):
# coding: iso-8859-1
(or encoding in characters present, latin-1 example.)
this line sets encoding file , allows use of special characters.
Comments
Post a Comment