【发布时间】:2014-01-04 08:19:01
【问题描述】:
我有 2 个大小为 data_large(150.1mb) 和 data_small(7.5kb) 的 json 文件。每个文件中的内容都是[{"score": 68},{"score": 78}] 类型。我需要从每个文件中找到唯一分数的列表。
在处理 data_small 时,我执行了以下操作,并且能够通过 0.1 secs 查看其内容。
with open('data_small') as f:
content = json.load(f)
print content # I'll be applying the logic to find the unique values later.
但是在处理 data_large 时,我执行了以下操作,我的系统被挂起,速度很慢,不得不强制关闭它以使其恢复正常速度。花了大约2 mins 打印它的内容。
with open('data_large') as f:
content = json.load(f)
print content # I'll be applying the logic to find the unique values later.
如何在处理大型数据集时提高程序的效率?
【问题讨论】:
-
对于大型 json 文件,请参阅:stackoverflow.com/questions/10382253/… 该答案建议 ijson
-
@vinod - 我不能使用 python 内置库吗?
-
json内置库一次加载整个文件。如果您需要对其进行迭代,那么您将需要手动解析 json 文件,或者只安装像ijson这样的库。 -
@python-coder 只需注释
print语句并使用data_large执行您的程序 -
@thefourtheye - 我评论了 print state ,但我需要再次强制关闭我的系统。上帝你会破坏我的系统。