【发布时间】:2020-08-05 11:24:57
【问题描述】:
我正在尝试将一个 json 字符串解析为 excel 文件。但面临一些错误
import pandas
...
...
response = requests.get(BASE_URL, headers=headers)
#print(response.text)
df_json = json.loads(response.text)
print(df_json) -- this is printing json as string
df = pd.read_json(df_json)
-- now i want to load this into excel
df.to_excel('c:\scripts\DATAFILE.xls', sheet_name='Sheet1', index=False, engine='xlsxwriter')
错误:
ValueError: Invalid file path or buffer object type: <class 'dict'>
有人可以帮忙吗
【问题讨论】:
-
你能分享一下你的打印结果吗(df_json)
-
使用
response.json()获取 JSON 而不是文本更正常。这假定 MIME 类型设置正确。分享print(df_json)的输出,以便为您的实际问题提供帮助 -
我猜我的 json 输出它自己乱七八糟,无法解析..需要弄清楚
-
错误很明显,你没有数据框而是字典。请分享minimal reproducible example