【问题标题】:got error TypeError: urlretrieve() got an unexpected keyword argument 'CablingFilename' python得到错误 TypeError: urlretrieve() got an unexpected keyword argument 'CablingFilename' python
【发布时间】:2014-09-24 22:21:56
【问题描述】:

我正在尝试从在线的 txt 文件中创建一个列表,这是代码的一部分

import urllib

def Dict(Filename,var1,var2):
    FileDict = open(Filename,'r')

    List = []
    for l in FileDict:
        if D in l:
            pattern = re.split('W',l)
            List.append(pattern[4])

更多代码...

    return (thing1,thing2,List)

in1=raw_input("name of file \n >")
url='https.page_address'

in2=raw_input("other instructions")
in3=raw_input("other instructions2")
urllib.urlretrieve(url+in1, Filename = in1)

MyDict = Dict(in1,in2,in3)

但是当我执行脚本并输入 in1(文件名)时,我收到错误“TypeError: urlretrieve() got an unexpected keyword argument 'Filename' 我反复检查但我不知道错误 希望有人帮助,我是 python 新手,所以请不要那么努力 "

【问题讨论】:

    标签: python url dictionary get typeerror


    【解决方案1】:

    关键字是小写的f,“文件名”不是文件名。 filename = uri

    错误的方式:

    In [56]: import urllib
    
    In [57]: in1 = "foo"
    
    In [58]: urllib.urlretrieve("http://goo.gl/xymu6o",Filename=uri)
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-58-e89982cacc95> in <module>()
    ----> 1 urllib.urlretrieve("http://goo.gl/xymu6o",Filename=in1)
    
    TypeError: urlretrieve() got an unexpected keyword argument 'Filename'
    

    正确方法:

    In [59]: import urllib
    
    In [60]: 
    
    In [60]: in1 = "foo"
    
    In [61]: urllib.urlretrieve("http://goo.gl/xymu6o",filename=in1 )
    Out[61]: ('foo', <httplib.HTTPMessage instance at 0x7fb580d57680>)
    

    只要改成urllib.urlretrieve(url+in1, filename = in1)

    urllib.urlretrieve(url+in1, in1)

    从文档描述中可以看出:

    urllib.urlretrieve(url[, filename[, reporthook[, data]]])
    

    您传递url 这是必需和可选的,filename 不是Filename

    filename 是关键字 arg,您不能使用 Filename,因为它根本不是有效的关键字参数。

    【讨论】:

    • 什么意思?它只是文件的名称,并且在所有脚本中都写了相同的“文件名”
    • Urlretrieve 采用关键字 arg 文件名,它指定将文件保存为什么。您将 Filename 作为关键字 arg,它不是有效的关键字。 f和f在python中是不一样的。
    • 对不起,我还是不明白,他们都是用F写的,所以我要从F改成f?我已经这样做了,但这是同样的错误
    • 您只需将其更改为 urlretrieve 中的文件名。你实际上甚至不需要使用 filename= 因为第二个参数默认是文件名
    • 我该怎么写呢?用户必须转到该页面并从文件列表中选择一个文件,这就是我这样写的原因,对不起,我在其他脚本中使用它并且它有效,这就是为什么我不知道如何使用你的想法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 2012-05-07
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 2015-09-23
    相关资源
    最近更新 更多