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.
Related
This question already has answers here:
Best way to convert string to bytes in Python 3?
(5 answers)
Closed 8 months ago.
The community reviewed whether to reopen this question 8 months ago and left it closed:
Original close reason(s) were not resolved
I am able to define a bytes string as follows:
string = b"0x340xEF"
I want to cast an already made string. Let us call it string into a bytes string. How do I perform this? bytes(string) is not giving me an equivalent result.
You need to encode the string to be able to convert it to bytes:
string = "b0x340xEF".encode("latin-1")
print(string)
Output:
b'b0x340xEF'
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
This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 2 years ago.
I cant seem to write something like : "C:\Users\alon4\Desktop\Learning Material\Odyssey\Lab a2\Magnetism\data"
As a string to print or otherwise, i think its something to do with the slashes
There can be 3 ways :
1) Either use r (raw strings) before the actual string :
For example : r"C:\Users\alon4\Desktop\Learning Material\Odyssey\Lab a2\Magnetism\data"
2) Or use // like:
For example : "C:\\Users\\alon4\\Desktop\\Learning Material\\Odyssey\\Lab a2\\Magnetism\data"
3) Use slashes: "C:/Users/alon4/Desktop/Learning Material/Odyssey/Lab a2/Magnetism/data"
(As suggested by Óscar López)
This question already has answers here:
How can I selectively escape percent (%) in Python strings?
(6 answers)
What is %% for in Python? [duplicate]
(1 answer)
Closed 4 years ago.
I came across a problem when I was learning python.
print('test%d, %.2f%%' % (1,1.4))
however, it has an error.
ValueError: incomplete format
But if I execute like this:
print('test%d, %.2f%%' % (1,1.4))
test1, 1.40%
It works and prints the '%'. But I don't know why? Can someone help me? Thanks.
Since % is used as a special character in (old C-style) format strings, you have to use %% to print a literal percent sign.
You need to look into c-style string formatting. %% is a reference to this series of string formatting commands.
The following page:
https://docs.python.org/3.4/library/string.html
has a "String formatting mini-language" section that answers your question in meticulous detail.
This question already has answers here:
How to convert hexadecimal string to bytes in Python?
(7 answers)
Closed 5 years ago.
I want to convert a hexadecimal string like 1030 to a byte array like b'\x10\x30'
I know we can use bytearray.fromhex("1030") or "1030".decode("hex"). However, I get output '\x100'.
What am I missing here?
bytearray(b'\x100') is correct, you just interpret it wrong way. It is character \x10 followed by character 0 (which happens to be ASCII for \x30).
There is a built-in function in bytearray that does what you intend.
bytearray.fromhex("de ad be ef 00")
It returns a bytearray and it reads hex strings with or without space separator.