Проблемы с Python с KeyError: пропустить ошибки, если данные отсутствуют в json? ⇐ Python
-
Anonymous
Проблемы с Python с KeyError: пропустить ошибки, если данные отсутствуют в json?
One is having trouble with a basic Python script that calls an API and then displays the info on a simple textbox. The issue is that sometimes, the Application Programming Interface (API) will have all the information, and sometimes it will not. When it has all the information, the script completes perfectly and displays all the information as laid out; however, sometimes the API does not have a specific section in it, which then causes a KeyError.
It is either required to completely skip over the area if that section is not in the API or preferably have a text that sits there and says "no info" and if the API has that section, it should be out.
Below is the script:
root = tk.Tk() root.geometry("760x760") root.title('Check API') # enter ID here entry = Entry(root, font = "Calibri 20") entry.pack() # Go check the API def Check_API(event): API = requests.get(f"URL hangs out here") # API Request url # Turn API into a json data = API.json() # Pull out the info we need from the json file # Full String for Troubleshooting Info = data # Main Info - allways in the json InfoNAME = data["name"] InfoCONN = data["connected"] InfoTIME = data["time"] # This info is only in the json sometimes - here is where the problem is InfoUPLOAD = data['up-loader'][0]["uploaded"] InfoUPTIME = data['up-loader'][0]["upload-time"] # Take info from above and load it onto the chart below # deletes all the text that is currently in the TextBox text_box.delete('1.0', END) # insert new data into the TextBox # full string text_box.insert(END, "Full String:\n") text_box.insert(END, Info) # Name text_box.insert(END, "Name: ") text_box.insert(END, InfoNAME) # Online status text_box.insert(END, "Status: ") text_box.insert(END, InfoCONN) # Last online Time text_box.insert(END, "Last Online: ") text_box.insert(END, InfoTIME) # Upload logger text_box.insert(END, "Was there an upload:") text_box.insert(END, InfoUPLOAD) # Upload time text_box.insert(END, "When was the upload: ") text_box.insert(END, InfoUPTIME) # Thats the lot, say bye to the nice people text_box.insert(END, "ok bieeeeeeeeeeeeeeeeeeeee ") text_box = Text(root, height=20, width=200, font = "Calibri 20") get_button = Button(root, text="Info Check", command=Check_API) root.bind('',Check_API) text_box.pack() get_button.pack() root.mainloop() I look forward to hearing from you.
Источник: https://stackoverflow.com/questions/780 ... n-the-json
One is having trouble with a basic Python script that calls an API and then displays the info on a simple textbox. The issue is that sometimes, the Application Programming Interface (API) will have all the information, and sometimes it will not. When it has all the information, the script completes perfectly and displays all the information as laid out; however, sometimes the API does not have a specific section in it, which then causes a KeyError.
It is either required to completely skip over the area if that section is not in the API or preferably have a text that sits there and says "no info" and if the API has that section, it should be out.
Below is the script:
root = tk.Tk() root.geometry("760x760") root.title('Check API') # enter ID here entry = Entry(root, font = "Calibri 20") entry.pack() # Go check the API def Check_API(event): API = requests.get(f"URL hangs out here") # API Request url # Turn API into a json data = API.json() # Pull out the info we need from the json file # Full String for Troubleshooting Info = data # Main Info - allways in the json InfoNAME = data["name"] InfoCONN = data["connected"] InfoTIME = data["time"] # This info is only in the json sometimes - here is where the problem is InfoUPLOAD = data['up-loader'][0]["uploaded"] InfoUPTIME = data['up-loader'][0]["upload-time"] # Take info from above and load it onto the chart below # deletes all the text that is currently in the TextBox text_box.delete('1.0', END) # insert new data into the TextBox # full string text_box.insert(END, "Full String:\n") text_box.insert(END, Info) # Name text_box.insert(END, "Name: ") text_box.insert(END, InfoNAME) # Online status text_box.insert(END, "Status: ") text_box.insert(END, InfoCONN) # Last online Time text_box.insert(END, "Last Online: ") text_box.insert(END, InfoTIME) # Upload logger text_box.insert(END, "Was there an upload:") text_box.insert(END, InfoUPLOAD) # Upload time text_box.insert(END, "When was the upload: ") text_box.insert(END, InfoUPTIME) # Thats the lot, say bye to the nice people text_box.insert(END, "ok bieeeeeeeeeeeeeeeeeeeee ") text_box = Text(root, height=20, width=200, font = "Calibri 20") get_button = Button(root, text="Info Check", command=Check_API) root.bind('',Check_API) text_box.pack() get_button.pack() root.mainloop() I look forward to hearing from you.
Источник: https://stackoverflow.com/questions/780 ... n-the-json