【问题标题】:Why do I always get this cPickle error all the time? [duplicate]为什么我总是得到这个 cPickle 错误? [复制]
【发布时间】:2016-07-08 05:11:18
【问题描述】:

我正在尝试将字典存储到文件中。

这是我的代码的一部分:

Accounts={}
if username not in Accounts:
    Accounts[username]=password
    database=open("my_mail_database", "w")
     pickle.dump(Accounts, database)
     database.close()

我总是得到这个错误:

Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1538, in __call__
return self.func(*args)
File "C:\Python34\python 3.4\my_gmail2.pyw", line 51, in submit_account
pickle.dump(Accounts, database)
TypeError: must be str, not bytes

谁能告诉我我的代码有什么问题?

【问题讨论】:

标签: python pickle


【解决方案1】:

试试看:

Accounts={}
if username not in Accounts:
    Accounts[username]=password
    database=open("my_mail_database", "wb")
    pickle.dump(Accounts, database)
    database.close()

你必须用 wb 打开文件

【讨论】:

    猜你喜欢
    • 2014-01-10
    • 1970-01-01
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 1970-01-01
    • 2017-09-18
    相关资源
    最近更新 更多