Parse file with multiple JSON objects in Python [duplicate] - python

This question already has answers here:
Loading and parsing a JSON file with multiple JSON objects
(5 answers)
Closed 3 years ago.
I want to parse a file that contains multiple JSON objects that are not enclosed in an array and are separated only by a line break. The file has the following schema:
{"id":1,"firstName":"John","lastName":"Doe"}
{"id":2,"firstName":"Bob","lastName":"Smith"}
As far as I know, the standard approach using json.load() doesn't work here, because the objects are not enclosed in an array. So is there an elegant way to parse such a file in Python without modifying it?

If every json object is on its own line, you should be able to do something like
with open('/path/to/file') as data:
objects = [json.loads(line) for line in data]

Related

How to search for keywords in a txt-file and print full string out [duplicate]

This question already has answers here:
How to search for a string in text files?
(13 answers)
Closed 1 year ago.
Hello i am using python combined with txt file and want to let my code search for parts of a word, like my txt file looks like this =
'www.test.com/product/wings' , 'www.test1.com/product/chicken' , 'www.okay.com/product/burger' , 'www.Test23542.com/product/smoothie'
So and i want to let my code open the file and search for 'www.test1.com/product/' and it should print out the complete link, included the product like = 'www.test1.com/product/chicken'
I am stuck and dont know how to do it so my current code wont really help because its just one line.
Here is an example. Use open and then read or readline to read in the date in the file and then use str() methods to find what you need,

strange python dictionary? [duplicate]

This question already has answers here:
What does the 'b' character do in front of a string literal?
(11 answers)
Convert a bytes array into JSON format
(8 answers)
Closed 2 years ago.
new to python here. Been trying this for quite a while now; would really appreciate some help.
I thought to try my hand at pulling data from an api, and what i got is a very long dictionary ( or at least i think its an dictionary )
response = requests.get('https://api.coingecko.com/api/v3/finance_products')
print(response.content)
can anyone give me any pointers on how to manipulate the results ?
I can't call the dictionary.
First few characters of it :
b'[{"platform":"Binance Savings","identifier":"CCOCOS30DAYSS001","supply_rate_percentage":"6.0","borrow_rate_percentage":null,"number_duration":null,"length_duration":null,"start_at":0,"end_at":0,"value_at":0,"redeem_at":0},{"platform":"DDEX Lending","identifier": etc etc
i'm not sure why there is a b' at the front.
Sorry if this isn't a clear question.
You can use
response.json()
which decodes it immediatly to a json object. Documentation here

Remove brackets from list [duplicate]

This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 5 years ago.
I have some problems with using a list in python.
Right now, I open a .txt file with data, and read it into my python file.
However, when I put the input from the datafile into variable data and print this to check if it works or not, I see a lot of extra brackets which I don't want. Right now, it looks like:
["['sports','pizza','other']"]
and I want it to have it in a way like this:
['sports','pizza','other']
Can someone help me to get this work? Reason why I want it in a format like I mentioned above, is that I want to compare the list with another list, and that does not work in the format with the ]"]
I hope someone will help me.
Simply use eval function from Python.
>>> a = ["['sports','pizza','other']"]
>>> eval(a[0])
['sports', 'pizza', 'other']

How to deserialize complex json string to python object? [duplicate]

This question already has answers here:
Deserialize a json string to an object in python
(12 answers)
Closed 8 years ago.
I need to convert json string to python object. for example,
{
"person":{
"name":"aa",
"age":"12",
"address":{
"city":"cc",
"road":"kk"
}
}
}
there are two python class Person and Address used to generate python object. but I don't know how to map it.
You can easily convert the JSON string to a native Python dictionary with json.loads:
import json
d = json.loads(s)
It is not clear what arguments your Person and Address take, but if they take keyword arguments matching the dictionary content it could be as simple as:
d['address'] = Address(**d['address'])
p = Person(**d)
Where ** unpacks the dictionary into keyword arguments.
You can do this in a couple ways. One way is the simplejson literal translation. I would say the cleanest way of creating an extensible method is creating a class with that same structure.

How can I convert words in [ ] to words in line? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to join list of strings?
I have a couple of functions in a module to run. First, I have to use read() to read strings in a document and save it as new_string and run a function. Then, I need to read a document using readlines(). After running the first function like match = clean_passage(new_string) then match contains ['/n', 'asdf', 'dfg']. Now, I need them in a line as it is shown in the original document. So, asdf dfg. How can I convet match into a thing that contains strings in a similar fashion that we get when we read a document using readlines().
So far, to do this, I had to save it and then open it using readlines(), which takes time. Is there any way to do that using a simple command? Sorry if the explanation is not clear.
try this:
to_convert = ['/n', 'asdf', 'dfg']
original_line = " ".join(to_convert)

Categories