python3.10中json读取问题,正常写入,读出现问题
# 写入代码
import json
numbers = [2, 3, 5, 7, 11, 13]
string1 = "hello world"
filename = 'numbers.json'
with open(filename, 'w') as f:
json.dump(numbers, f)
json.dump(string1, f)
# 读入代码
import json
file_name = 'numbers.json'
with open(file_name) as f:
numbers = json.load(f)
print('1'.center(20,'-'))
print(numbers)
运行结果及报错内容
Traceback (most recent call last):
File "/home/helmer/scripts/python3/json_read.py", line 6, in <module>
numbers = json.load(f)
File "/usr/lib/python3.10/json/__init__.py", line 293, in load
return loads(fp.read(),
File "/usr/lib/python3.10/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.10/json/decoder.py", line 340, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 21 (char 20)
在read()和write()方法中进行写入和读入操作没有问题