[英]Fetching information from the different links on a web page and writing them to a .xls file using pandas,bs4 in Python
[英]writing json string to xls file using python pandas
我正在尝试将一个 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'>
有人可以帮忙吗
pd.read_json()
将文件路径或 JSON 字符串作为输入。 您应该检查df_json
的类型,因为json.loads()
反序列化输入。 如果它是一个 dict 你可以简单地做
df = pd.DataFrame(df_json)
如果它是一个列表,它会更复杂一些。
如果它们不是数据框,我也会避免在变量名“df”前面加上前缀。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.