这将适用于您提供的特定文件格式。如果格式更改,则需要更改分析行的方式。{
"key11": 11,
"key12": 12
}
{
"key21": 21,
"key22": 22
}
只需逐行阅读,并在运行时构建JSON块:with open(args.infile, 'r') as infile:
# Variable for building our JSON block
json_block = []
for line in infile:
# Add the line to our JSON block
json_block.append(line)
# Check whether we closed our JSON block
if line.startswith('}'):
# Do something with the JSON dictionary
json_dict = json.loads(''.join(json_block))
print(json_dict)
# Start a new block
json_block = []
如果您对解析一个非常大的JSON文件而不将所有内容保存到内存感兴趣,那么您应该查看JSON.load API中的object_hook或object_pairs_hook回调方法。