[英]writing json string to xls file using python pandas
I am trying to parse one json string to excel file.我正在尝试将一个 json 字符串解析为 excel 文件。 But facing some errors
但面临一些错误
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')
Error:错误:
ValueError: Invalid file path or buffer object type: <class 'dict'>
can some one help please有人可以帮忙吗
pd.read_json()
takes a file path or a JSON string as input. pd.read_json()
将文件路径或 JSON 字符串作为输入。 You should check the type of df_json
because json.loads()
deserializes the input.您应该检查
df_json
的类型,因为json.loads()
反序列化输入。 If it is a dict you could simply do如果它是一个 dict 你可以简单地做
df = pd.DataFrame(df_json)
If it's a list it's a bit more complex.如果它是一个列表,它会更复杂一些。
Also I would refrain from prefixing variable names "df" if they aren't dataframes.如果它们不是数据框,我也会避免在变量名“df”前面加上前缀。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.