以前正常工作的Pandas read_json脚本现在会产生错误

flseospp  于 2023-06-20  发布在  其他
关注(0)|答案(1)|浏览(116)

我有一个脚本,直到最近工作正常,但现在正在产生一个错误。

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

我该如何解决此问题?

mi7gmzs6

mi7gmzs61#

您可以将df = pd.read_json(rawdata)更改为df = pd.read_json(io.StringIO(rawdata.decode('utf-8')))
您还需要在文件的前面包含import io

相关问题