Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse binary string to dict ?

I have flask-service. Sometimes I can get json message without a point at http header. In this case I'm trying to parse message from request.data. But the string from request.data is really hard thing to parse. It's a binary string like this:

b'{\n    "begindate": "2016-11-22", \n    "enddate": "2016-11-22", \n    "guids": ["6593062E-9030-B2BC-E63A-25FBB4723ECC", \n              "5A9F8478-6673-428A-8E90-3AC4CD764543", \n              "D8243BA1-0847-48BE-9619-336CB3B3C70C"]\n}'

When I'm trying to use json.loads(), I'm getting this error:

TypeError: the JSON object must be str, not 'bytes'

Function of converting to string (str()) doesn't work good too:

'b\'{\\n    "begindate": "2016-11-22", \\n    "enddate": "2016-11-22", \\n    "guids": ["6593062E-9030-B2BC-E63A-25FBB4723ECC", \\n              "5A9F8478-6673-428A-8E90-3AC4CD764543", \\n              "D8243BA1-0847-48BE-9619-336CB3B3C70C"]\\n}\''

I use Python 3. What can I do to parse request.data ?

like image 519
faoxis Avatar asked Jan 12 '17 06:01

faoxis


People also ask

How do you decode a binary string?

Remember that in binary 1 is "on: and 0 is "off." Choose the binary number that you want to decode. Give each number a value, starting from the extreme right. For example, using the number 1001001, 1=1, +0=2, +0=4, +1=8, +0=16, +0=32, +1=64.

How do I read a binary string in Python?

Method #1: The binary data is divided into sets of 7 bits because this set of binary as input, returns the corresponding decimal value which is ASCII code of the character of a string. This ASCII code is then converted to string using chr() function.

How do I convert a string to a dictionary in Python?

eval() is an inbuilt python library function used to convert string to dictionary efficiently. For this approach, you have to import the ast package from the python library and then use it with the literal_eval() method.

How do I change a string to a dictionary?

Method 1: Splitting a string to generate key:value pair of the dictionary In this approach, the given string will be analysed and with the use of split() method, the string will be split in such a way that it generates the key:value pair for the creation of a dictionary.


1 Answers

Just decode it before passing it to json.loads:

b = b'{\n    "begindate": "2016-11-22", \n    "enddate": "2016-11-22", \n    "guids": ["6593062E-9030-B2BC-E63A-25FBB4723ECC", \n              "5A9F8478-6673-428A-8E90-3AC4CD764543", \n              "D8243BA1-0847-48BE-9619-336CB3B3C70C"]\n}'
r = json.loads(b.decode())
print(r)
{'begindate': '2016-11-22',
 'enddate': '2016-11-22',
 'guids': ['6593062E-9030-B2BC-E63A-25FBB4723ECC',
  '5A9F8478-6673-428A-8E90-3AC4CD764543',
  'D8243BA1-0847-48BE-9619-336CB3B3C70C']}

Python 3.x makes a clear distinction between the types:

  • str = '...' literals = a sequence of Unicode characters (UTF-16 or UTF-32, depending on how Python was compiled)

  • bytes = b'...' literals = a sequence of octets (integers between 0 and 255)

Link for more info

like image 91
Dimitris Fasarakis Hilliard Avatar answered Nov 10 '22 19:11

Dimitris Fasarakis Hilliard