[英]Concatenate string to list of dictionaries in python
I am trying to join a string to a list of dictionaries.我正在尝试将字符串加入字典列表。 I am doing:
我在做:
print(site + ', '.join(search_res))
I keep getting an error: sequence item 0: expected str instance, dict found
我不断收到错误:
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"
Expected result:预期结果:
"Fantasy" , [
{
"book": "Harry Potter",
"rating": "10.0"
},
{
"book": "Lord of The Rings",
"rating": "9.0"
}
]
How do I concatenate the string to the list of dictionaries without getting the sequence item 0: expected str instance, dict found
error如何在不获取
sequence item 0: expected str instance, dict found
情况下将字符串连接到字典列表sequence item 0: expected str instance, dict found
错误
Why not just print(site + str(search_res))
?为什么不只是
print(site + str(search_res))
?
Also you could do: print(site + ', '.join([str(dic) for dic in search_res)])
你也可以这样做:
print(site + ', '.join([str(dic) for dic in search_res)])
也许你只需要这个: print(site + ' , ' + str(search_res))
How do I concatenate the string to the list of dictionaries
如何将字符串连接到字典列表
You can't.你不能。 And you don't need to either.
你也不需要。 Use string formatting , possibly together with the
pprint
module .使用字符串格式化,可能与
pprint
模块一起使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.