簡體   English   中英

如何使用 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM