我试图在
Python中读取大型JSON文件(data.json).因为JSON文件有多个JSON对象,并且将在Python中创建多个字典(字典的数量未知),所以我使用了decoder.raw_decode()和generator.
以下是代码:
import json
def parse():
with open('data.json',encoding='utf-8') as jfile:
try:
while True:
decoder = json.JSONDecoder()
obj, idx = decoder.raw_decode(jfile)
yield obj
except ValueError as e:
print(e)
pass
else:
print("aha")
def main():
imputd=parse()
if imputd:
while True:
try:
print(str(next(imputd)).readlines())
except StopIteration as e:
print(e)
break
main()
我收到错误:
Traceback (most recent call last):
File "H:\Document\Python\j10.py", line 57, in
main()
File "H:\Document\Python\j10.py", line 36, in main
print(str(next(imputd)).readlines())