I have a script that up until recently worked fine, but is now producing an error.
import requests
import pandas as pd
# Set the url to given endpoint
url = "https://SomeURL/SomeEndpoint"
print('URL set')
# Connect to endpoint with credentials and put results in dictionary
URLresponse = requests.get(url,auth=("SomeUser", "SomePassword"), verify=True)
print('connection to endpoint')
# Load the response as proper JSON into a var
rawdata = (URLresponse.content)
print(type(rawdata))
print('populating variable')
# print(rawdata)
# Load the var into a dataframe
df = pd.read_json(rawdata)
print('load variable into df')
print(df)
This used to work fine but now it is producing an error as following:
File "C:\Program Files\Python310\lib\site-packages\pandas\io\common.py", line 901, in get_handle
raise TypeError(
TypeError: Expected file path name or file-like object, got <class 'bytes'> type
How can I go ahead to troubleshoot this?
>Solution :
You can change df = pd.read_json(rawdata) to df = pd.read_json(io.StringIO(rawdata.decode('utf-8')))
You will need to include import io earlier in your file as well.