I am partially able to work with json saved as file:
#! /usr/bin/python3
import json
from pprint import pprint
json_file='a.json'
json_data=open(json_file)
data = json.load(json_data)
json_data.close()
print(data[10])
But I am trying to achieve the same from data directly from web. I am trying with the accepted answer here:
#! /usr/bin/python3
from urllib.request import urlopen
import json
from pprint import pprint
jsonget=urlopen("http://api.crossref.org/works?query.author=Rudra+Banerjee")
data = json.load(jsonget)
pprint(data)
which is giving me error:
Traceback (most recent call last):
File "i.py", line 10, in <module>
data = json.load(jsonget)
File "/usr/lib64/python3.5/json/__init__.py", line 268, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/usr/lib64/python3.5/json/__init__.py", line 312, in loads
s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'
What is going wrong here?
Changing the code as par Charlie's reply to:
jsonget=str(urlopen("http://api.crossref.org/works?query.author=Rudra+Banerjee"))
data = json.load(jsonget)
pprint(jsonget)
breaks at json.load:
Traceback (most recent call last):
File "i.py", line 9, in <module>
data = json.load(jsonget)
File "/usr/lib64/python3.5/json/__init__.py", line 265, in load
return loads(fp.read(),
AttributeError: 'str' object has no attribute 'read'
It's actually telling you the answer: you're getting back a byte array, where in Python 3 a string is different because of dealing with unicode. In Python 2.7, it would work. You should be able to fix it by converting your bytes explicitly to a string with
jsonget=str(urlopen("http://api.crossref.org/works?query.author=Rudra+Banerjee")_
Related
I am working on a project using the CBOR file to contain data. I already install cbor with pip install cbor. But I cannot read it.
This is my code:
import cbor
cbor.loads(r"C:\Users\User\Desktop\project\score.cbor")
Then its return this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\User\Anaconda3\envs\pyenv\lib\site-packages\cbor\cbor.py", line 263, in loads
fp = StringIO(data)
TypeError: a bytes-like object is required, not 'str'
How to solve this problem? And is there a way to convert CBOR to JSON file because I find it easier to work with the JSON file in Python.
I still don't know how to solve this problem with cbor, I use cbor2 instead and it works.
import cbor2
path = "home/data/score.cbor"
with open(path, 'rb') as fp:
obj = cbor2.load(fp)
print(obj)
Output
{'root': {'probe': 20, 'candidate': 31}, 'tree': [], 'support': []}
I have an issue with json on Python3.
I try to get the "pairs" from string of URL: https://wex.nz/api/3/info
This is my code:
import urllib.request
import json
url= 'https://wex.nz/api/3/info'
content=urllib.request.urlopen(url)
for line in content:
liste=json.loads(line)
pairliste=liste['pairs']
print(pairliste)
This is my error:
Traceback (most recent call last):
File "/home/lennart/Documents/Test/Test.py", line 8, in <module>
liste=json.loads(line)
File "/usr/lib/python3.5/json/__init__.py", line 312, in loads
s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'
Thanks to roganjosh.
This fixed my issue:
import urllib.request
import json
url= 'https://wex.nz/api/3/info'
content = urllib.request.urlopen(url)
data =json.loads(content.read().decode())
print(data)
When I run the following code:
import shelve
input = open("input.txt",)
shelveFile = shelve.open("myData")
shelveFile["inputFile"] = input
input.close()
shelveFile.close()
I expect the shelve file myData to hold the file object input. Instead, running the code produces the following error:
Traceback (most recent call last):
File "/Users/ashutoshmishra/Documents/Sandbox/Sandbox3.py", line 5, in <module>
shelveFile["inputFile"] = input
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/shelve.py", line 124, in __setitem__
p.dump(value)
TypeError: cannot serialize '_io.TextIOWrapper' object
I was wondering why I could not save the file object input to the shelve file myData?
The following answer is taken from #DanD.'s comment above.
Read the file: shelveFile["inputFile"] = input.read()
I'm trying a simple json.load
import urllib.request
import json
response = urllib.request.urlopen('https://www.stubhub.com/ticketAPI/restSvc/event/4100000/sort/price/0/')
data = json.load(response)
and am getting the following error
Traceback (most recent call last):
File "C:\Python33\lib\http\client.py", line 590, in _readall_chunked
chunk_left = self._read_next_chunk_size()
File "C:\Python33\lib\http\client.py", line 562, in _read_next_chunk_size
return int(line, 16)
ValueError: invalid literal for int() with base 16: '\n'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\test.py", line 5, in <module>
data = json.load(url)
File "C:\Python33\lib\json\__init__.py", line 271, in load
return loads(fp.read(),
File "C:\Python33\lib\http\client.py", line 509, in read
return self._readall_chunked()
File "C:\Python33\lib\http\client.py", line 594, in _readall_chunked
raise IncompleteRead(b''.join(value))
http.client.IncompleteRead: IncompleteRead(16384 bytes read)
Based on the error I can't really figure out what might be going on here. Could anyone shed some light on how to get around this?
Edit:
Using the requests module works, but I'd rather use the core Python modules if possible.
import requests
import json
r = requests.get('https://www.stubhub.com/ticketAPI/restSvc/event/4100000/sort/price/0/')
data = json.loads(r.text)
This works fine for me
import urllib.request
import json
response = urllib.request.urlopen('https://www.stubhub.com/ticketAPI/restSvc/event/4100000/sort/price/0/')
data = json.loads(response.read().decode('utf-8'))
I have the following code to load JSON:
import json
import requests
r = requests.get('http://api.reddit.com/controversial?limit=5')
if r.status_code = 200:
reddit_data = json.loads(r.content)
print reddit_data['data']['children'][1]['data']
else:
print "Errror."
And I got this message.
arsh#arsh:~$ python q.py
Traceback (most recent call last):
File "q.py", line 1, in <module>
import json
File "/home/arsh/json.py", line 5, in <module>
reddit_data = json.loads(r.content)
AttributeError: 'module' object has no attribute 'loads'
You have a different file called json.py in your home directory:
File "/home/arsh/json.py", line 5, in <module>
This file is in the way, you did not import the standard library version. Rename it to something else or delete it. You'll also have to remove the json.pyc file.
Note that requests response objects can already handle JSON responses for you:
import requests
r = requests.get('http://api.reddit.com/controversial?limit=5')
r.raise_for_status()
reddit_data = r.json()
print reddit_data['data']['children'][1]['data']
The Response.json() method handles decoding JSON for you, including detecting the correct characterset to use when decoding.