AttributeError:“tuple”对象没有“strip”属性

2024-09-27 17:30:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用python,这是我的代码

        myUser = 'username'
        myServer = 'http://api.url.net', "{\"orga\":\"monorga\",\"coupon\":\"moncoupon\"}"
        myPass = 'pass'
        authString = base64.encodestring('%s:%s' % (myUser, myPass))
        headers = {'Authorization':"Basic %s" % authString}
        req = urllib2.Request(myServer, None, headers)
        openedUrl = urllib2.urlopen(req)

url = url.strip()
AttributeError: 'tuple' object has no attribute 'strip'

请帮帮我


Tags: 代码apihttpurlnetusernameurllib2req
1条回答
网友
1楼 · 发布于 2024-09-27 17:30:00

当你写url = 'https://api.url.net', "{\"orga\":\"monorga\",\"coupon\":\"moncoupon\"}"的时候,基本上你只需要创建一个tuple的两个元素('https://api.url.net'"{\"orga\":\"monorga\",\"coupon\":\"moncoupon\"}"),并将这个元组的引用分配给url变量。这是因为,在python中,并不总是需要编写元组的括号:a = 1, 2

因此,url现在是元组。另外,元组没有strip方法,因此不能调用url.strip

要在url上调用strip,必须首先将其转换为字符串。

相关问题 更多 >

    热门问题