您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

5b51 2022/1/14 8:22:42 python 字数 2826 阅读 555 来源 www.jb51.cc/python

当尝试应用一些我在i Python上在互联网上找到的代码时,会出现一个错误: TypeError Traceback (most recent call last) <ipython-input-4-36ec95de9a5d> in <module>() 13 all[i] = r.json() 1

概述

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:\John\Footy Bants\R 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/

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

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

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

总结

以上是编程之家为你收集整理的python – 试图写一个cPickle对象,但是得到一个’write’属性类型的错误全部内容,希望文章能够帮你解决python – 试图写一个cPickle对象,但是得到一个’write’属性类型的错误所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶