strange python dictionary? [duplicate] - python

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

Related

numpy.ndarray object's name only converted to a string [duplicate]

This question already has answers here:
python - name of np array variable as string
(2 answers)
Closed 9 months ago.
If I wish to get the name of a numpy.ndarray converted to a string (but not the content of the numpy.ndarray, just the name only), how do I do that?
I tried str(npndarrayName) and npndarrayName.tostring() but both are converting the content and not the name itself only.
I am not sure if I fully understood what you're asking, but you can check this link because I think they had the same question as you.
I hope that it will solve your problem.

reversing a section of string in python with [start:stop:step] [duplicate]

This question already has answers here:
Understanding negative steps in list slicing
(2 answers)
Closed 1 year ago.
I am learning python and I can't solve this problem:
I am ok to reverse the whole string but I want to get only the "Hello" part
astring = "Hello world!"
I was expecting print(astring[0:4:-1]) would do the work but it does not.
print(astring[5:0:-1]) is better but the H is still missing. I get "olle"
Is there anyway to solve this?
Thank you,
Try this:
print(astring[4::-1])

Python 3 - Hex-String to bytes [duplicate]

This question already has an answer here:
Python: Converting HEX string to bytes
(1 answer)
Closed 2 years ago.
I am using python 3 and try to convert a hex-string to a byte-represented form. So i used the following command:
bytes.fromhex('97ad300414b64c')
I Expected results like this: b'\x97\xad\x30\x04\x14\xb6\x4c'' but got b'\x97\xad0\x04\x14\xb6L'. I am note sure what i am doing wrong, but maybe it is something with the encoding?
As pointed by #user8651755 in the comments, this is due to the fact that some bytes correspond to printable characters. So the answer is: you are doing everything right.

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 find the position of every instance of a character in a list [duplicate]

This question already has answers here:
How do i find the position of MORE THAN ONE substring in a string (Python 3.4.3 shell)
(3 answers)
Closed 6 years ago.
I have a program where I need to identify the location of every instance of the letter A in a quote. Something like I would do with quote.index("A"), but I need every instance of A, not just the first.
I know this question has been asked before but I'm very, very new to Python and I'm having trouble understanding the answers to those questions.
If anyone could give me a dumbed down explanation of how to do this, I'd be incredibly thankful because I'm utterly lost.
If i understand correctly, you have a string and you want to keep all A's locations in e different array.
Then you can try something like that.
quote = "some quote"
locs = []
for i in range(len(quote)) :
if quote[i] == 'A' :
locs.append(i)
print(locs)

Categories