如何在Python中使用ijson解析JSON时检索文件位置?

问题描述 投票:0回答:1

我正在使用Python中的ijson库来解析一个大的JSON文件,我需要找到文件中特定数据所在的位置。我想使用 file.tell() 在解析过程中获取文件读取器的当前位置。但它只给我文件的长度。

from ijson import parse
with open('file','r') as f:
 for a, b, c in parse(f):
  print(f.tell())
python ijson
1个回答
0
投票

ijson.parse
正在使用源文件的缓冲读取:

>>> help(ijson.parse)
Help on function parse in module ijson.common:

parse(source, buf_size=65536, **config)

如果您使用

parse(f, buf_size=1)
f.tell()
应该是准确的,但解析可能会更慢。

© www.soinside.com 2019 - 2024. All rights reserved.