我正在尝试解析一个大的 json 文件(数百个演出)以从其键中提取信息。为简单起见,请考虑以下示例:
import random, string
# To create a random key
def random_string(length):
return "".join(random.choice(string.lowercase) for i in range(length))
# Create the dicitonary
dummy = {random_string(10): random.sample(range(1, 1000), 10) for times in range(15)}
# Dump the dictionary into a json file
with open("dummy.json", "w") as fp:
json.dump(dummy, fp)
然后,我在 python 2.7 中使用 ijson 来解析文件:
file_name = "dummy.json"
with open(file_name, "r") as fp:
for key in dummy.keys():
print "key: ", key
parser = ijson.items(fp, str(key) + ".item")
for number in parser:
print number,
我期待检索与 dic 键对应的列表中的所有数字。然而,我得到了
IncompleteJSONError:不完整的 JSON 数据
我知道这篇文章:Using python ijson to read a large json file with multiple json objects,但在我的情况下,我有一个格式良好的单个 json 文件,具有相对简单的模式。关于如何解析它的任何想法?谢谢你。