![](/img/trans.png)
[英]Merge multiple JSON files into one file by using Python (stream twitter)
[英]Optimal way to parse Twitter JSON objects from one file/multiple files into python
我有 Twitter 數據集(多個 JSON 文件),但讓我們從一個文件開始。 我必須將 JSON 對象解析為 python 但json.loads()
只解析一個 ZA8CFDE6331BD49EB2AC96F866 此處提出了類似的問題,但解決方案不起作用或不夠好。
1- 我無法將 JSON 對象轉換為列表,因為它效率不高而且我有太多數據。 還提出的解決方案基於“\n”,而我的 Twitter 數據對象以}{
沒有newline
,我無法手動添加。 (Twitter 對象也不是一行一行的)
2- 第二種解決方案是JSONStream
,官方文檔中沒有太多關於它的信息。
3-還有其他有效的方法嗎? 我考慮的一個是使用MongoDB
。 但我從未在MongoDB
上工作過。 所以我不知道這是否可行。
下圖顯示了推文 object 和}{
的長度
with open('sampledata.json','r',encoding='utf8') as json_file:
#for i in json_file:
while(True):
dataobj = json.load(json_file)
print(dataobj)
print("Printing each JSON Decoded Object")
錯誤:因為一個 object 有 287 行。
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 287 column 2 (char 10528)
不需要在讀取 json 文件時使用的while
循環您可以使用它來讀取 json 文件:
def read_json(path):
with open(path, 'r') as file:
return json.load(file)
my_data = read_json('sampledata.json')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.