4

我正在学习如何在 txt/dat 文件中读取/写入字符串、列表等。我想在我的代码中添加注释,以便我可以参考访问密钥是什么。所以这就是我所做的。

# 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()

当我运行它时,我收到以下错误:

SyntaxError:第 10 行文件 PickleIt.py 中以 '\x92' 开头的非 UTF-8 代码,但未声明编码;有关详细信息,请参见http://python.org/dev/peps/pep-0263/

我查看了链接,但我真的不明白,我以前没见过这个。


哦,抱歉第 10 行是# rb

4

3 回答 3

8
于 2013-08-01T06:55:09.957 回答
2
于 2013-08-01T06:54:07.640 回答
0

this character seems to be the problem ’</p>

于 2013-08-01T06:56:17.967 回答