gpt4 book ai didi

python - 将对象数组连接到 Python 中的字符串值

转载 作者:行者123 更新时间:2023-12-01 00:17:30 25 4
gpt4 key购买 nike

我想将对象数组连接到 python 中的字符串。我有什么办法可以做到这一点吗?

url =  "https://google.com",
search = "thai food",
results = [
{
"restaurant": "Siam Palace",
"rating": "4.5"
},
{
"restaurant": "Bangkok Palace",
"rating": "3.5"
}
]

我希望能够将这些全部结合起来形成一个值。

如果我能让它看起来像:

 data = {    url =  "https://google.com", 
{
search = "thai food",
results = [
{
"restaurant": "Siam Palace",
"rating": "4.5"
},
{
"restaurant": "Bangkok Palace",
"rating": "3.5"
}
]
}
}

我从 mongodb 收到这些结果,并希望将这 3 个结果连接在一起。

最佳答案

使用 JSON 模块

data = {} # create empty dict

# set the fields
data['url'] = 'https://google.com'
data['search'] = 'thai food'

# set the results
data['results'] = results

# export as string
import json
print(json.dumps(data, indent=4)

关于python - 将对象数组连接到 Python 中的字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59219497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com