Join and format array of objects in Python

2024/10/6 22:32:16

I want to join and format values and array of objects to a string in python. Is there any way for me to do that?

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"}
]

I want to be able to format the values as such:

If I could make it look like:

            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"}]}]}, ]

I did this to join the values:

        data = {} data['url'] = 'https://google.com'data['search'] = 'thai food'data['results'] = resultsimport jsonprint(json.dumps(data, indent=4)    

My results are joining the 3 values all together and repeats it for each of them. Is there anyway for me to format it in the format mentioned above?

Answer

You could make a list and append each entry.

    all_data = []data_google = {'url' = 'https://google.com','results' = []}data_thai = {}data_thai['search'] = 'thai food'data_thai['results'] = resultsdata_google.append(data_thai)data_indian = {}data_indian['search'] = 'indian food'data_indian['results'] = resultsdata_google.append(data_indian)all_data.append(data_google)...import jsonprint(json.dumps(all_data , indent=4) 
https://en.xdnf.cn/q/118894.html

Related Q&A

Copying text from file to specified Excel column [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 6…

Name error: Variable not defined

Program calculates the shortest route from point, to line, then to second point. Also I need to say how long is from the start of the line, to where point crosses. My code so far: from math import sqrt…

Error while deploying flask app on apache

I have a file manage.py, import os from app import create_app app = create_app(os.getenv(FLASK_CONFIG) or default) if __name__ == __main__:app.run()manage.py is working fine when tested in debug mode. …

Selenium Python get_element by ID failing

Can someone help me understand why my code fails to find the element by ID. Code below:from selenium import webdriver driver=webdriver.Firefox() driver.get(https://app.waitwhile.com/checkin/lltest3/use…

Pipelining POST requests with python-requests

Assuming that I can verify that a bunch of POST requests are in fact logically independent, how can I set up HTTP pipelining using python-requests and force it to allow POST requests in the pipeline?D…

How to take a whole matrix as a input in Python?

I want to take a whole matrix as an input in Python and store it in a dataframe. Pandas can do it automatically with read_csv function but it requires a CSV file. I want to input/copy-paste a matrix di…

Cannot create environment in anaconda, update conda , install packages

CondaHTTPError: HTTP 000 CONNECTION FAILED for url https://repo.anaconda.com/pkgs/free/win-64/repodata.json.bz2 Elapsed: -An HTTP error occurred when trying to retrieve this URL. HTTP errors are often …

Inverted Triangle in Python-not running

I have to create a program to print an inverted triangle in python. When I was running it in Sublime Text 3 it did not run. By that, I mean that it did not even print a syntax error. def triangle():x =…

How to do Data profile to a table using pandas_profiling

When Im trying to do data profiling one sql server table by using pandas_profiling throwing an error like An attempt has been made to start a new process before thecurrent process has finished its boot…

Python replace line by index number

Is it possible in Python to replace the content of a line in a file by its index number?Would something like a line.replace to do this procedure?