如何使用 Pickle 模块序列化字符串? (Python)

How to serialize strings with the Pickle module? (Python)

每当我尝试这样做时,都会出现错误:

TypeError: write() argument must be str, not bytes

我使用的代码是:

        with open("save.pckl", 'w') as f:
        pickle.dump(name, f)

有谁知道为什么会这样? Pickle 只是不支持序列化字符串吗?提前致谢!

您以 'w' 模式打开文件。要使用 pickle.dump,您应该以写入二进制模式 wb:

打开它
with open("save.pckl", 'wb') as f:
    pickle.dump(name, f)