Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request returns bytes and I'm failing to decode them

Essentially I made a request to a website and got a byte response back: b'[{"geonameId:"703448"}..........'. I'm confused because although it is of type byte, it is very human readable and appears like a list of json. I do know that the response is encoded in latin1 from running r.encoding which returned ISO-859-1 and I have tried to decode it, but it just returns an empty string. Here's what I have so far:

r = response.content string = r.decode("ISO-8859-1") print (string) 

and this is where it prints a blank line. However when I run

len(string) 

I get: back 31023 How can I decode these bytes without getting back an empty string?

like image 309
koda gates Avatar asked Jul 29 '15 18:07

koda gates


People also ask

How do you decode bytes?

decode() is used to decode bytes to a string object. Decoding to a string object depends on the specified arguments. It also allows us to mention an error handling scheme to use for seconding errors. Note: bytes is a built-in binary sequence type in Python.

How do you return bytes in Python?

The bytes() function returns a bytes object. It can convert objects into bytes objects, or create empty bytes object of the specified size. The difference between bytes() and bytearray() is that bytes() returns an object that cannot be modified, and bytearray() returns an object that can be modified.


2 Answers

Did you try to parse it with the json module?

import json parsed = json.loads(response.content) 
like image 159
mzc Avatar answered Sep 29 '22 19:09

mzc


Another solution is to use response.text, which returns the content in unicode

Type:        property String form: <property object at 0x7f76f8c79db8> Docstring:   Content of the response, in unicode.  If Response.encoding is None, encoding will be guessed using ``chardet``.  The encoding of the response content is determined based solely on HTTP headers, following RFC 2616 to the letter. If you can take advantage of non-HTTP knowledge to make a better guess at the encoding, you should set ``r.encoding`` appropriately before accessing this property. 
like image 30
salah Avatar answered Sep 29 '22 19:09

salah