加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 试图写一个cPickle对象,但是得到一个’write’属性类

发布时间:2020-12-16 23:18:08 所属栏目:Python 来源:网络整理
导读:当尝试应用一些我在i Python上在互联网上找到的代码时,会出现一个错误: TypeError Traceback (most recent call last) ipython-input-4-36ec95de9a5d in module() 13 all[i] = r.json() 14 --- 15 cPickle.dump(all,outfile)TypeError: argument must have
当尝试应用一些我在i Python上在互联网上找到的代码时,会出现一个错误:
TypeError                                 Traceback (most recent call last)
    <ipython-input-4-36ec95de9a5d> in <module>()
     13     all[i] = r.json()
     14 
---> 15 cPickle.dump(all,outfile)

TypeError: argument must have 'write' attribute

这是我按顺序完成的:

outfile = "C:JohnFooty BantsR COMPLAEX MATHS"

然后,我粘贴了以下代码:

import requests,cPickle,shutil,time

all = {}
errorout = open("errors.log","w")

for i in range(600):
    playerurl = "http://fantasy.premierleague.com/web/api/elements/%s/"
    r = requests.get(playerurl % i)

    # skip non-existent players
    if r.status_code != 200: continue

    all[i] = r.json()

cPickle.dump(all,outfile)

这是原始文章,让您了解我正在尝试实现的目标:

http://billmill.org/fantasypl/

解决方法

cPickle.dump()的第二个参数必须是文件对象.你传递一个包含文件名的字符串.

您需要使用open()函数打开该文件名的文件对象,然后将文件对象传递给cPickle:

with open(outfile,'wb') as pickle_file:
    cPickle.dump(all,pickle_file)

参见Python教程的Reading and Writing Files section,包括为什么在打开文件时使用它是一个好主意(它会自动关闭).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读