在Python中将对象数组与字符串值连接起来

2024-06-28 11:41:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在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个连接在一起


Tags: 对象httpscomurlsearchfoodgooglerestaurant
1条回答
网友
1楼 · 发布于 2024-06-28 11:41:07

使用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)

相关问题 更多 >