python - 将字符串连接到python中的字典列表
问题描述
我正在尝试将字符串加入字典列表。我在做:
print(site + ', '.join(search_res))
我不断收到错误消息:sequence item 0: expected str instance, dict found
search_res = [
{
"book": "Harry Potter",
"rating": "10.0"
},
{
"book": "Lord of The Rings",
"rating": "9.0"
}
]
site = "Fantasy"
预期结果:
"Fantasy" , [
{
"book": "Harry Potter",
"rating": "10.0"
},
{
"book": "Lord of The Rings",
"rating": "9.0"
}
]
如何将字符串连接到字典列表而不会出现sequence item 0: expected str instance, dict found
错误
解决方案
为什么不只是print(site + str(search_res))
?
你也可以这样做:print(site + ', '.join([str(dic) for dic in search_res)])
推荐阅读
- javascript - 如何通过下载文件
- swift - 为什么我的 if else 语句的 else 部分没有执行?
- javascript - Javascript ip地址比较
- python - 加载图像像素数据会产生意想不到的结果 - Python - Pillow
- javascript - 在paperJS中将自定义对象添加到组
- modelsim - Modelsim - 编译按钮什么都不做
- teamcity - 基于已安装的 Powershell 模块的 TeamCity 构建代理资格?
- python - python错误:元组索引必须是整数或切片而不是元组
- python-3.x - 在熊猫数据框中查找包含各种类型列表(列表)的重复行
- spring-boot - Keycloak 资源保护建议