【问题标题】:pickle error - integer required泡菜错误 - 需要整数
【发布时间】:2017-08-16 14:00:29
【问题描述】:
with open('data', 'w') as f:
    pickle.dumps({'foo':111},f)

结果

an integer is required (got type _io.TextIOWrapper)

我该如何解决这个问题?

我很确定 An integer is required? open() 没有事先被调用。 Python版本为3.6.2

【问题讨论】:

标签: python-3.x pickle


【解决方案1】:

pickle.dumpsobj 转储到它返回的字符串中。为了写入文件,您可能需要使用pickle.dump(不带 s)。

with open('data', 'wb') as f:
    pickle.dump({'foo':111}, f)

另外你还应该以二进制模式打开文件,因为pickle.dump会写入二进制数据。

【讨论】:

  • 我刚刚编辑了打开文件时提到二进制模式的答案。
猜你喜欢
  • 2018-09-09
  • 1970-01-01
  • 1970-01-01
  • 2016-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多