我正在尝试解析一个大的 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)
Run Code Online (Sandbox Code Playgroud)
然后,我在 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 …Run Code Online (Sandbox Code Playgroud)