我尝试从字符串中删除撇号...
您可以使用以下命令从字符串中删除撇号s = s.replace("'", "")
. .replace() http://docs.python.org/library/stdtypes.html#str.replace返回一个新字符串,但不会更改原始字符串,因此您将存储返回的字符串。
>>> string = "he's a jolly good fellow"
>>> string = string.replace("'", "")
>>> string
'hes a jolly good fellow'
但我不认为这不是你的问题。
The ids 需要用逗号分隔 https://dev.twitter.com/docs/api/1/get/users/lookup#api-param-lookup_user_ids,所以你可能想要使用.join() http://docs.python.org/library/stdtypes.html#str.join从您的 id 列表中创建字符串。例子:
>>> ids = ["1", "23", "123"]
>>> ",".join(ids)
'1,23,123'
在你的情况下,假设followers['ids']
包含 id 列表作为字符串,您可以使用以下方式生成 URL:
ids = ",".join(followers['ids']) # generate string of ids (comma separated)
url = "https://api.twitter.com/1.1/users/lookup.json?user_id=%s" % ids
If followers['ids']
是一个整数列表而不是字符串,那么还有更多工作要做.join()
仅适用于字符串序列。这是将这些整数即时转换为字符串的一种方法(使用生成器表达式 http://www.python.org/dev/peps/pep-0289/):
ids = ",".join(str(id) for id in followers['ids'])