首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将对象数组连接到Python中的字符串值

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

Stack Overflow用户
提问于 2019-12-06 19:52:30
回答 1查看 221关注 0票数 0

我希望将对象数组加入到python中的字符串中。有什么办法让我这么做吗?

代码语言:javascript
复制
url =  "https://google.com",
search = "thai food",
results = [
        {
            "restaurant": "Siam Palace",
            "rating": "4.5"
        },
        {
            "restaurant": "Bangkok Palace",
            "rating": "3.5"
        }
]

我希望能够将所有这些联系起来,形成一个值。

如果我能让它看起来像:

代码语言:javascript
复制
 data = {    url =  "https://google.com", 
{
   search = "thai food",
   results = [
        {
            "restaurant": "Siam Palace",
            "rating": "4.5"
        },
        {
            "restaurant": "Bangkok Palace",
            "rating": "3.5"
        }
]
}
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-06 19:58:08

使用JSON模块

代码语言:javascript
复制
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)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59219497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档