[英]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.