这不是关于读取大型JSON文件,而是关于以最有效的方式读取大量JSON文件。在
问题
我正在使用来自Million song dataset的last.fm数据集。
数据以一组JSON编码的文本文件的形式提供,其中的键是:track_id、artist、title、timestamp、similar和tags。在
目前,我在经过几个选项后,通过以下方式将它们读入pandas,因为这是最快的,如图here:import os
import pandas as pd
try:
import ujson as json
except ImportError:
try:
import simplejson as json
except ImportError:
import json
# Path to the dataset
path = "../lastfm_train/"
# Getting list of all json files in dataset
all_files = [os.path.join(root,file) for root, dirs, files in os.walk(path) for file in files if file.endswith('.json')]
data_list=[json.load(open(file)) for file in all_files]
df = pd.DataFrame(data_list, columns=['similars', 'track_id'])
df.set_index('track_id', inplace=True)
在当前方法中读取小于1的完整数据集。然而,阅读完整的火车集太慢了,而且要花很多时间(我也等了几个小时)才能阅读,并且已经成为了进一步任务的瓶颈,如question here所示。在
我还在解析json文件时使用ujson,这可以从this question here明显看出
更新1
使用生成器而不是理解列表。在
^{pr2}$