Python: Error messages that can't resolve [duplicate] - python
This question already has answers here:
HTTPResponse object -- JSON object must be str, not 'bytes'
(4 answers)
Closed 7 years ago.
I am new to python. I am using python 3.x. I have tried to correct this code many times but I am getting few error messages. Can someone please help to correct me with the code?
import urllib.request as urllib2
#import urllib2
#import urllib2
import json
def printResults(data):
#Use the json module to load the string data into a directory
theJSON = json.loads(data)
#Now we can access the contents of json like any other python object
if "title" in theJSON["metadata"]:
print (theJSON["metadata"]["title"])
def main():
#Define the varible that hold the source of the Url
urlData= "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson"
#Open url and read the data
webUrl= urllib2.urlopen(urlData)
#webUrl= urllib.urlopen(urldata)
print (webUrl.getcode())
if (webUrl.getcode() == 200):
data= webUrl.read()
#Print our our customized result
printResults(data)
else:
print ("Received an error from the server, can't retrieve results " + str(webUrl.getcode()))
if __name__== "__main__":
main()
Here are the errors that I am getting:
Traceback (most recent call last):
File "C:\Users\bm250199\workspace\test\JSON_Data.py", line 30, in <module>
main()
File "C:\Users\bm250199\workspace\test\JSON_Data.py", line 25, in main
printResults(data)
File "C:\Users\bm250199\workspace\test\JSON_Data.py", line 8, in printResults
theJSON = json.loads(data)
File "C:\Users\bm250199\AppData\Local\Programs\Python\Python35-32\lib\json\__init__.py", line 312, in loads
s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'
Just have to tell python to decode the bytes object it got into a string.
This can be done by using the decode function.
theJSON = json.loads(data.decode('utf-8'))
You could make the function more robust by adding an if condition like:
def printResults(data):
if type(data) == bytes: # Convert to string using utf-8 if data given is bytes
data = data.decode('utf-8')
#Use the json module to load the string data into a directory
theJSON = json.loads(data)
#Now we can access the contents of json like any other python object
if "title" in theJSON["metadata"]:
print (theJSON["metadata"]["title"])
Related
Reading a Json in python [duplicate]
This question already has answers here: Reading JSON from a file [duplicate] (7 answers) Closed 4 years ago. I need to get a particular key:value from a json file. Code used to open json file : import json data = repr(open('file.json', 'rb').read()) print(data) Output( Json file) : b'b\'{"image":{"width":750,"height":1127,"orientation":1},"objects":[{"type":"face","boundingBox":{"x":132,"y":180,"height":619,"width":513},"landmarks":{"faceContour":[[162,375],[162,440],[165,504],[175,568],[193,628],[226,686],[265,740],[313,784],[373,799],[436,793],[494,758],[543,712],[586,660],[616,601],[631,534],[639,468],[645,400]],"noseBridge":[[387,396],[385,441],[381,485],[378,532]],"noseBall":[[322,547],[350,560],[380,572],[412,563],[442,554]],"eyebrowRight":[[419,348],[466,329],[516,325],[564,339],[595,377]],"eyebrowLeft":[[197,358],[224,325],[269,315],[316,325],[358,347]],"eyeRight":[[448,419],[479,404],[510,405],[539,416],[511,425],[480,424]],"eyeRightCenter":[[495,416]],"eyeLeft":[[242,402],[269,393],[299,396],[329,412],[297,414],[267,413]],"eyeLeftCenter":[[284,405]],"mouthOuter":[[269,596],[310,598],[349,598],[379,606],[411,601],[456,606],[502,609],[456,663],[412,684],[378,687],[345,680],[307,652]],"mouthInner":[[284,603],[349,610],[379,615],[411,613],[487,614],[412,652],[379,656],[348,648]]},"attributes":{"gender":"male","genderConfidence":0.8385,"age":24,"ageConfidence":0.8419,"emotion":"happiness","emotionConfidence":1.0,"emotionsAll":{"neutral":0.0,"sadness":0.0,"disgust":0.0,"anger":0.0,"surprise":0.0,"fear":0.0,"happiness":1.0},"pose":{"pitch":-5.7105,"roll":0.2941,"yaw":1.8646},"race":{"asian":0.0002,"black":0.0001,"white":0.9989},"eyewear":{"sunglasses":0.0,"glasses":0.0},"hair":{"color":{"blond":0.0,"black":0.9994,"brown":0.0},"cut":{"short":0.9999,"long":0.0,"bald":0.0},"facial":{"beard":0.0,"mustache":0.0,"stubble":0.2543}},"frontal":true}},{"type":"person","boundingBox":{"x":20,"y":60,"height":1053,"width":696}}],"requestId":"1239be0c782440bd828cee21dbc2baa1"}\'\r\n' From this Json file, I need to extract a particular key:value. For example, I need to get the emotionsAll (Key) from the file. I tried using : print(data['emotionsAll'][0]['neutral']) error i got : Traceback (most recent call last): File "C:\Users\HP\Downloads\Final Project\getfile.py", line 6, in <module> print(data['emotionsAll'][0]['neutral']) TypeError: string indices must be integers So, Is there any other way to get a particular key:value ?
Try using the json package Contents of file.json {"image":{"width":750,"height":1127,"orientation":1},"objects":[{"type":"face","boundingBox":{"x":132,"y":180,"height":619,"width":513},"landmarks":{"faceContour":[[162,375],[162,440],[165,504],[175,568],[193,628],[226,686],[265,740],[313,784],[373,799],[436,793],[494,758],[543,712],[586,660],[616,601],[631,534],[639,468],[645,400]],"noseBridge":[[387,396],[385,441],[381,485],[378,532]],"noseBall":[[322,547],[350,560],[380,572],[412,563],[442,554]],"eyebrowRight":[[419,348],[466,329],[516,325],[564,339],[595,377]],"eyebrowLeft":[[197,358],[224,325],[269,315],[316,325],[358,347]],"eyeRight":[[448,419],[479,404],[510,405],[539,416],[511,425],[480,424]],"eyeRightCenter":[[495,416]],"eyeLeft":[[242,402],[269,393],[299,396],[329,412],[297,414],[267,413]],"eyeLeftCenter":[[284,405]],"mouthOuter":[[269,596],[310,598],[349,598],[379,606],[411,601],[456,606],[502,609],[456,663],[412,684],[378,687],[345,680],[307,652]],"mouthInner":[[284,603],[349,610],[379,615],[411,613],[487,614],[412,652],[379,656],[348,648]]},"attributes":{"gender":"male","genderConfidence":0.8385,"age":24,"ageConfidence":0.8419,"emotion":"happiness","emotionConfidence":1.0,"emotionsAll":{"neutral":0.0,"sadness":0.0,"disgust":0.0,"anger":0.0,"surprise":0.0,"fear":0.0,"happiness":1.0},"pose":{"pitch":-5.7105,"roll":0.2941,"yaw":1.8646},"race":{"asian":0.0002,"black":0.0001,"white":0.9989},"eyewear":{"sunglasses":0.0,"glasses":0.0},"hair":{"color":{"blond":0.0,"black":0.9994,"brown":0.0},"cut":{"short":0.9999,"long":0.0,"bald":0.0},"facial":{"beard":0.0,"mustache":0.0,"stubble":0.2543}},"frontal":true}},{"type":"person","boundingBox":{"x":20,"y":60,"height":1053,"width":696}}],"requestId":"1239be0c782440bd828cee21dbc2baa1"} Code: import json with open('file.json', 'rb') as fp: json_data = json.load(fp) print(json_data['objects'][0]['attributes']['emotionsAll']['neutral'])
A couple things: 1 - the emotionsAll key is within the objects key, first element in the list [0], attributes key 2 - Your json file was written with the bytes prefix, so when it's being read, it starts your string with b'. You can either a) have that file written without that mode by decoding/encoding, or just manipulate that string. import json data = repr(open('file.json', 'rb').read()) data = data.split('{', 1)[-1] data = data.rsplit('}', 1)[0] data = ''.join(['{', data, '}']) jsonObj = json.loads(data) print(jsonObj['objects'][0]['attributes']['emotionsAll']['neutral']) Output: print(jsonObj['objects'][0]['attributes']['emotionsAll']['neutral']) 0.0
Getting error while creating multiple file in python
I'm creating two files using python script, first file is JSON and second one is HTML file, my below is creating json file but while creating HTML file I'm getting error. Could someone help me to resolve the issue? I'm new to Python script so it would be really appreciated if you could suggest some solution #!/usr/bin/python # -*- coding: utf-8 -*- import sys import json JsonResponse = '[{"status": "active", "due_date": null, "group": "later", "task_id": 73286}]' def create(JsonResponse): print JsonResponse print 'creating new file' try: jsonFile = 'testFile.json' file = open(jsonFile, 'w') file.write(JsonResponse) file.close() with open('testFile.json') as json_data: infoFromJson = json.load(json_data) print infoFromJson htmlReportFile = 'Report.html' htmlfile = open(htmlReportFile, 'w') htmlfile.write(infoFromJson) htmlfile.close() except: print 'error occured' sys.exit(0) create(JsonResponse) I used below online Python editor to execute my code: https://www.tutorialspoint.com/execute_python_online.php
infoFromJson = json.load(json_data) Here, json.load() will expect a valid json data as json_data. But the json_data you provided are not valid json, it's a simple string(Hello World!). So, you are getting the error. ValueError: No JSON object could be decoded Update: In your code you should get the error: TypeError: expected a character buffer object That's because, the content you are writing to the file needs to be string, but in place of that, you have a list of dictionary. Two way to solve this. Replace the line: htmlfile.write(infoFromJson) To either this: htmlfile.write(str(infoFromJson)) To make infoFromJson a string. Or use the dump utility of json module: json.dump(infoFromJson, json_data)
If you delete Try...except statement, you will see errors below: Traceback (most recent call last): File "/Volumes/Ithink/wechatProjects/django_wx_joyme/app/test.py", line 26, in <module> create(JsonResponse) File "/Volumes/Ithink/wechatProjects/django_wx_joyme/app/test.py", line 22, in create htmlfile.write(infoFromJson) TypeError: expected a string or other character buffer object Errors occurred because htmlfile.write need string type ,but infoFromJson is a list . So,change htmlfile.write(infoFromJson) to htmlfile.write(str(infoFromJson)) will avoid errors!
json2html python lib is not working
I'm trying to create new json file with my custom json input and converting JSON to HTML format and saving into .html file. But I'm getting error while generating JSON and HTML file. Please find my below code - Not sure what I'm doing wrong here: #!/usr/bin/python # -*- coding: utf-8 -*- from json2html import * import sys import json JsonResponse = { "name": "json2html", "description": "Converts JSON to HTML tabular representation" } def create(JsonResponse): #print JsonResponse print 'creating new file' try: jsonFile = 'testFile.json' file = open(jsonFile, 'w') file.write(JsonResponse) file.close() with open('testFile.json') as json_data: infoFromJson = json.load(json_data) scanOutput = json2html.convert(json=infoFromJson) print scanOutput htmlReportFile = 'Report.html' htmlfile = open(htmlReportFile, 'w') htmlfile.write(str(scanOutput)) htmlfile.close() except: print 'error occured' sys.exit(0) create(JsonResponse) Can someone please help me resolve this issue. Thanks!
First, get rid of your try / except. Using except without a type expression is almost always a bad idea. In this particular case, it prevented you from knowing what was actually wrong. After we remove the bare except:, we get this useful error message: Traceback (most recent call last): File "x.py", line 31, in <module> create(JsonResponse) File "x.py", line 18, in create file.write(JsonResponse) TypeError: expected a character buffer object Sure enough, JsonResponse isn't a character string (str), but is a dictionary. This is easy enough to fix: file.write(json.dumps(JsonResponse)) Here is a create() subroutine with some other fixes I recommend. Note that writing the dumping the JSON followed immediately by loading the JSON is usually silly. I left it in assuming that your actual program does something slightly different. def create(JsonResponse): jsonFile = 'testFile.json' with open(jsonFile, 'w') as json_data: json.dump(JsonResponse, json_data) with open('testFile.json') as json_data: infoFromJson = json.load(json_data) scanOutput = json2html.convert(json=infoFromJson) htmlReportFile = 'Report.html' with open(htmlReportFile, 'w') as htmlfile: htmlfile.write(str(scanOutput))
The error is while writing to the JSON file. Instead of file.write(JsonResponse) you should use json.dump(JsonResponse,file). It will work.
Reading a JSON string in Python: Receiving error " TypeError: string indices must be integers"
I am trying to create a program that will get me current weather using the OpenWeatherMap API. I am new to coding in the sense of coding while receiving the data from the internet. The error I receive is: "Traceback (most recent call last): File "/home/pi/Python Codes/Weather/CurrentTest3.py", line 7, in temp_k = [record['temp'] for record in url2 ['main']] #this line should take down the temperature information from the .Json file File "/home/pi/Python Codes/Weather/CurrentTest3.py", line 7, in temp_k = [record['temp'] for record in url2 ['main']] #this line should take down the temperature information from the .Json file TypeError: string indices must be integers I do not understand why I am getting this, my code is below. from dateutil import parser #imports parser from pprint import pprint #imports pprint import requests #imports request url = requests.get('http://api.openweathermap.org/data/2.5/weather? q=london&APPID=APIKEY') #identifies the url address pprint(url.json()) #prints .JSON information from address url2 = url.json() #establishes .Json file as variable temp_k = [record['temp'] for record in url2 ['main']] #this line should take down the temperature information from the .Json file print(temp_k) #prints the value for temperature
The value for main in your data is a dict, not a list of dicts. So there is no need to iterate through it; just access the temp value directly. temp_k = url2['main']['temp']
The problem is with this portion of temp_k, record['temp']. This is the format of each variable record: for record in url2 ['main']: print record >> pressure temp_min temp_max temp humidity It is a bunch of strings that you are trying to index as a dictionary, hence the string indices error. Just change the temp_k line to this: temp_k = [url2['main'].get('temp')] >> [272.9]
Unable to print the contents in the File using "urllib2"
I'm trying to print the lines present in the url file however,I'm getting an error that says: with html as ins: AttributeError: __exit__ Below posted is my code import urllib2 response = urllib2.urlopen('------------------') html = response.read() counter = 0; with html as ins: array = [] for line in ins: counter = counter+1 print "cluster number is:", counter print line
If you want to write the bytes from the url as is (no decoding/encoding): #!/usr/bin/env python2 import urllib2 import shutil import sys from contextlib import closing with closing(urllib2.urlopen(url)) as response: shutil.copyfileobj(response, sys.stdout) It expects that the character encoding used by response is the same character encoding your terminal uses otherwise you'll see mojibake. See A good way to get the charset/encoding of an HTTP response in Python. Your code in the question contains multiple errors e.g.: wrong indentation it tries to use a str object as a context manager that leads to AttributeError (__exit__ method is not defined) because str object does not implement the context manager protocol for line in ins is misleading: iterating over a string yields characters, not lines.