json 逐条存入总是报错,遇到不少坑,下面是自己测试可以使用的代码
使用缩进(indent=2)便于查看,读取方法:
import json
import os
from random import sample
# import time
def save_load_json(save_dir,filename='data.json',save_data=None):
'''
save_dir:存时表示文件夹
filename:存入文件夹名
savedata:save_data is not None
loaddata:if save_data == None
读取文件时:传入目标文件地址
'''
if save_data!=None:
if not os.path.exists(save_dir):
os.makedirs(save_dir)
with open(os.path.join(save_dir,filename),'a',encoding='utf-8') as f:
json_str = json.dumps(save_data,ensure_ascii=False,indent=2)
# json.dump(save_data,f,ensure_ascii=False,indent=2)
f.write(json_str)
f.write(',\n')
print('already saved in {}'.format(os.path.join(save_dir,filename)))
f.close()
return None
else:
with open('{}'.format(save_dir),'r') as f:
data = json.load(f)
print('already load {}'.format(save_dir))
f.close()
return data
但是读取会报错,如下:
---------------------------------------------------------------------------
JSONDecodeError Traceback (most recent call last)
Cell In[3], line 1
----> 1 data = save_load_json('/exp1_ins0_4shot.json',"",None)
Cell In[2], line 27, in save_load_json(save_dir, filename, save_data)
25 else:
26 with open('{}'.format(save_dir),'r') as f:
---> 27 data = json.load(f)
28 print('already load {}'.format(save_dir))
29 f.close()
File /usr/lib/python3.9/json/__init__.py:293, in load(fp, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
274 def load(fp, *, cls=None, object_hook=None, parse_float=None,
275 parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):
276 """Deserialize ``fp`` (a ``.read()``-supporting file-like object containing
277 a JSON document) to a Python object.
278
(...)
291 kwarg; otherwise ``JSONDecoder`` is used.
292 """
--> 293 return loads(fp.read(),
294 cls=cls, object_hook=object_hook,
295 parse_float=parse_float, parse_int=parse_int,
296 parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File /usr/lib/python3.9/json/__init__.py:346, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
341 s = s.decode(detect_encoding(s), 'surrogatepass')
343 if (cls is None and object_hook is None and
344 parse_int is None and parse_float is None and
345 parse_constant is None and object_pairs_hook is None and not kw):
--> 346 return _default_decoder.decode(s)
347 if cls is None:
348 cls = JSONDecoder
File /usr/lib/python3.9/json/decoder.py:340, in JSONDecoder.decode(self, s, _w)
338 end = _w(s, end).end()
339 if end != len(s):
--> 340 raise JSONDecodeError("Extra data", s, end)
341 return obj
JSONDecodeError: Extra data: line 4 column 2 (char 545)
遇到这种情况 找了很多解决方案,比如逐条读出,key双引号都没有解决问题。
后来观察比赛数据的json格式,发现只要在文件两头加上’['']‘,
用大括号把逐条存入数据括起来就解决问题了。
最后一行逗号要删除。
改前
改后
注意逐条存入在两条数据间需要写入 ‘,\n’
正确的格式如下:
还有个方法就是别用indent,然后逐条读取 方法如下:(测试过了 可以使用)
import json
import os
from random import sample
# import time
def save_load_json(save_dir,filename='data.json',save_data=None):
'''
save_dir:存时表示文件夹
filename:存入文件夹名
savedata:save_data is not None
loaddata:if save_data == None
读取文件时:传入目标文件地址
'''
if save_data!=None:
if not os.path.exists(save_dir):
os.makedirs(save_dir)
with open(os.path.join(save_dir,filename),'a',encoding='utf-8') as f:
json_str = json.dumps(save_data,ensure_ascii=False)
# json.dump(save_data,f,ensure_ascii=False)
f.write(json_str)
f.write('\n')
print('already saved in {}'.format(os.path.join(save_dir,filename)))
f.close()
return None
else:
data = []
with open(data_dir, encoding='utf-8') as f:
while True:
line = f.readline()
if not line: # 到 EOF,返回空字符串,则终止循环
break
js = json.loads(line)
data.append(js)
f.close()
return data
数据格式:
头部这样
尾部是没有逗号的
但是最近又爆了个读取bug 有些莫名其妙,改天写