You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python遍历列表拼接URL触发TypeError,求问题原因及解决方法

解决 TypeError: can only concatenate str (not "list") to str 问题

你遇到的问题根源很直接:你试图把一个列表直接和字符串做拼接操作了。你的extensionid是一个包含两个字符串的列表(['1234','12356']),但Python里字符串只能和另一个字符串完成拼接,列表不支持直接和字符串相加,这就触发了这个类型错误。

要实现你遍历列表、逐个处理每个extensionid的需求,你需要用循环遍历列表里的每个元素,对每个单独的ID生成对应的URL后再发起请求。这里给你两种修正后的代码方案:

方案1:普通字符串拼接

extension_ids = ['1234','12356']  # 重命名变量让语义更清晰
params = { 'dndStatus': "TakeAllCalls", }

# 遍历每个extension id
for ext_id in extension_ids:
    # 用单个id字符串拼接URL
    url = '/restapi/v1.0/account/~/extension/' + ext_id + '/presence'
    resp = platform.get(url, params,)
    print(resp.text())

方案2:用f-string(更简洁易读)

Python 3.6+支持的格式化字符串字面值(f-string),写起来更清爽直观:

extension_ids = ['1234','12356']
params = { 'dndStatus': "TakeAllCalls", }

for ext_id in extension_ids:
    url = f'/restapi/v1.0/account/~/extension/{ext_id}/presence'
    resp = platform.get(url, params,)
    print(resp.text())

这样循环处理每个ID,就能避免类型错误,同时完成你想要的遍历列表发起请求的功能啦。

内容的提问来源于stack exchange,提问作者MasterPhilip

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:25:43