TypeError: can't use a string pattern on a bytes-like object

json, python, python-3.x, string

Solution

If you use Python 3.x, you should pass `str` object to `json.loads`.

Replace following line:

print(json.loads(line))

with:

print(json.loads(line.decode()))

UPDATE: The behavior changed in Python 3.6. The argument can now be of type bytes or bytearray. The input encoding should be UTF-8, UTF-16 or UTF-32.

Problem

``` import json import requests url = 'http://developer.usa.gov/1usagov.json' r = requests.get(url, stream=True) for line in r.iter_lines(): if line: print (json.loads(line)) ``` Gives this error: ``` TypeError: can't use a string pattern on a bytes-like object ``` While viewing through the browser i do see that the response is a Json but request library says its a bytes like object why so ?

Original source

Related problems