python - 在Python中读取大型JSON文件

标签 python json sed

我有一个大约 5GB 的大型 JSON 文件,但它不是由一个 JSON 文件组成,而是由多个连接在一起的。

{"created_at":"Mon Jan 13 20:01:57 +0000 2014","id":422820833807970304,"id_str":"422820833807970304"}
{"created_at":"Mon Jan 13 20:01:57 +0000     2014","id":422820837545500672,"id_str":"422820837545500672"}.....

大括号之间没有换行 }{.

我尝试使用 sed 将大括号替换为换行符,然后使用以下命令读取文件:

data=[]
for line in open(filename,'r').readline():
data.append(json.loads(line))

但这行不通。

如何才能相对快速地读取该文件?

非常感谢任何帮助

最佳答案

这是一个黑客行为。它不会将整个文件加载到内存中。我真的希望你使用 Python 3。

DecodeLargeJSON.py

from DecodeLargeJSON import *
import io
import json

# create a file with two jsons
f = io.StringIO()
json.dump({1:[]}, f)
json.dump({2:"hallo"}, f)
print(repr(f.getvalue()))
f.seek(0) 

# decode the file f. f could be any file from here on. f.read(...) should return str
o1, idx1 = json.loads(FileString(f), cls = BigJSONDecoder)
print(o1) # this is the loaded object
# idx1 is the index that the second object begins with
o2, idx2 = json.loads(FileString(f, idx1), cls = BigJSONDecoder)
print(o2)

如果您发现某些对象无法解码,可以告诉我,我们可以找到解决方案。

免责声明这不是有效且最佳的解决方案。这是一个展示如何使其成为可能的黑客。

讨论 因为它不会将整个文件加载到内存中,所以正则表达式不起作用。它还使用 Python 实现而不是 C 实现。这可能会使速度变慢。我真的很讨厌这个简单的任务却如此困难。希望其他人指出不同的解决方案。

关于python - 在Python中读取大型JSON文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22900804/

相关文章:

python - 使用 py2neo (Neo4j) 自动递增属性?

python - Matplotlib 方形箱线图

javascript - 以自定义方式显示和操作状态值

linux - 使用具有模式映射的外部文件替换模式后的行

python - 如何更快地在文本文件中搜索字符串

从 tar.gz 导入 Python 库?

Unix sed 命令 - 在 2 个斜杠之间提取文本

linux - 每月获取第一个和最后一个文件

javascript - 我有 json 格式的时间段,并希望按照书面描述在适当的时间段进行转换

javascript - 如果整个 DOM 使用相同的 div,则仅将字符串附加到特定的 div 一次