首页 > 解决方案 > 在 Python 中加入和格式化对象数组

问题描述

我想将值和对象数组连接并格式化为python中的字符串。我有什么办法吗?

url =  "https://google.com",
search = "thai food",
search_res = [
        {
            "restaurant": "Siam Palace",
            "rating": "4.5"
        },
        {
            "restaurant": "Bangkok Palace",
            "rating": "3.5"
        }
]
url =  "https://google.com",
search = "indian food",
search_res = [
        {
            "restaurant": "Taj Palace",
            "rating": "2.5"
        },
        {
            "restaurant": "Chennai Express",
            "rating": "5.0"
        }
]
url =  "https://bing.com",
search = "thai food",
search_res = [
        {
            "restaurant": "Siam Palace",
            "rating": "1.5"
        },
        {
            "restaurant": "Bangkok Palace",
            "rating": "4.5"
        }
]
url =  "https://bing.com",
search = "indian food",
search_res = [
        {
            "restaurant": "Taj Palace",
            "rating": "4.5"
        },
        {
            "restaurant": "Chennai Express",
            "rating": "3.0"
        }
]

我希望能够像这样格式化值:

如果我能让它看起来像:

            all_data = [{
              url = "https://google.com",
              results = [{
                search = "thai food",
                search_res = [{
                  "restaurant": "Siam Palace",
                  "rating": "4.5"
                }, {
                  "restaurant": "Bangkok Palace",
                  "rating": "3.5"
                }]
              }, {
                search = "Indian food",
                search_res = [{
                  "restaurant": "Taj Palace",
                  "rating": "2.5"
                }, {
                  "restaurant": "CHennai Express",
                  "rating": "5.0"
                }]
              }]
            }, {
              url = "https://bing.com",
              results = [{
                search = "thai food",
                search_res = [{
                  "restaurant": "Siam Palace",
                  "rating": "1.5"
                }, {
                  "restaurant": "Bangkok Palace",
                  "rating": "4.5"
                }]
              }, {
                search = "Indian food",
                search_res = [{
                  "restaurant": "Taj Palace",
                  "rating": "4.5"
                }, {
                  "restaurant": "CHennai Express",
                  "rating": "3.0"
                }]
              }]
            }, ]

我这样做是为了加入价值观:

        data = {} 
        data['url'] = 'https://google.com'
        data['search'] = 'thai food'
        data['results'] = results

        import json
        print(json.dumps(data, indent=4)    

我的结果是将 3 个值全部连接在一起并为每个值重复它。无论如何我可以将其格式化为上述格式吗?

标签: pythonjsondjangomongodbpymongo

解决方案


您可以制作一个列表并附加每个条目。

    all_data = []

    data_google = {
      'url' = 'https://google.com',
      'results' = []
    }
    data_thai = {}
    data_thai['search'] = 'thai food'
    data_thai['results'] = results
    data_google.append(data_thai)

    data_indian = {}
    data_indian['search'] = 'indian food'
    data_indian['results'] = results
    data_google.append(data_indian)

    all_data.append(data_google)

    ...

    import json
    print(json.dumps(all_data , indent=4) 

推荐阅读