簡體   English   中英

Pandas read_json ValueError:協議未知

[英]Pandas read_json ValueError: Protocol not known

我剛才運行了以下代碼並且它有效但現在出現以下錯誤。 如何解決?

ValueError:協議未知。

import json
temp = json.dumps([status._json for status in tweet])  # create JSON
newdf = pd.read_json(temp, orient='records')

在我的案例中,解決方案包括使用StringIO ,如下所示:

from io import StringIO
newdf = pd.read_json(StringIO(temp))

看起來 Pandas 1.1 中的pd.read_json不再接受簡單的字符串。

據我所知,這個問題是由熊貓更新引起的。 1.1.0 更新在 read_json 函數上改變了一些東西。

將熊貓版本設置為 1.0.5 時,我可以使我的代碼工作

https://pandas.pydata.org/docs/whatsnew/v1.1.0.html

我同意 ehabets

from io import StringIO
df = pd.read_json(StringIO(json_demo))

現在,pd.read_json 需要文件類型的 json。 這意味着 pd.read_json("xxx.json") 可以工作。

你的代碼:

import json
temp = json.dumps([status._json for status in tweet]) #create JSON
newdf = pd.read_json(temp, orient='records')

修復代碼:

import json
from io import StringIO
temp = StringIO(json.dumps([status._json for status in tweet])) #create JSON
df = pd.read_json(StringIO(temp, orient='records')

它適用於熊貓==1.1.3

作為參考,如果上述方法失敗,另一種方法:

通過 Microsoft Excel 將現有 Excel 文件轉換為 .XLSX 格式並保存

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM