我有一个脚本,直到最近工作正常,但现在正在产生一个错误。
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)
这曾经工作正常,但现在它正在产生如下错误:
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
我该如何解决此问题?
1条答案
按热度按时间mi7gmzs61#
您可以将
df = pd.read_json(rawdata)
更改为df = pd.read_json(io.StringIO(rawdata.decode('utf-8')))
您还需要在文件的前面包含
import io
。