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

Python中如何将JSON响应中的文件名存入字符串列表?

问题分析与解决

错误原因

JSON响应里文件名对应的键是**fileName(驼峰命名,第二个字母为大写N),但你代码里写的是filename**(全小写),键名大小写不匹配,导致抛出KeyError。

修正后的代码

fileNames = []
for file in response.json()["files"]:
    fileNames.append(file["fileName"])

也可以用更简洁的列表推导式实现:

fileNames = [file["fileName"] for file in response.json()["files"]]

预期结果

执行修正后的代码后,会得到目标文件名列表:

['abc.mp4', 'def.mp4', 'ghi.mp4']

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 20:40:54