我正在學習如何將字符串,列表等寫入txt/dat文件。我想在我的代碼中添加註釋,以便我可以參考訪問密鑰的內容。所以這就是我所做的。Python - 試圖添加註釋時出錯
# Mode Description
# rb Read from a binary file. If the file doesn’t exist, Python will complain with an error.
# wb Write to a binary file. If the file exists, its contents are overwritten. If the file doesn’t exist,
# it’s created.
# ab Append a binary file. If the file exists, new data is appended to it. If the file doesn’t exist, it’s
# created.
# rb+ Read from and write to a binary file. If the file doesn’t exist, Python will complain with an
# error.
# wb+ Write to and read from a binary file. If the file exists, its contents are overwritten. If the file
# doesn’t exist, it’s created.
# ab+ Append and read from a binary file.
後,我有:
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()
當我運行它,我得到以下錯誤:
語法錯誤:非UTF-8編碼與 '\ X92' 文件PickleIt開始.py在第10行,但沒有聲明編碼;詳情請見http://python.org/dev/peps/pep-0263/
我查了一下鏈接,但是我真的不明白,以前我沒看過。
哦和道歉線10 # rb
刪除評論中的'apostrophes'然後再試 – suhailvs
哇,固定它,謝謝。我刪除並替換了它們。這是否意味着python或ide沒有識別出我複製過的? – mccdlibby
它不是'utf-8'支持的字符,我認爲它是'ascii'或者什麼 – suhailvs