python) json 쓰기 오류날 때

yeonsoo·2022년 7월 27일
0
class NpEncoder(json.JSONEncoder):
   def default(self, obj):
       if isinstance(obj, np.integer):
           return int(obj)
       if isinstance(obj, np.floating):
           return float(obj)
       if isinstance(obj, np.ndarray):
           return obj.tolist()
       return super(NpEncoder, self).default(obj)

위 클래스 선언해주고 아래처럼 cls로 넣어줌

json_path = os.path.join(args.project_dir, '4step.json')
with open(json_path, 'w') as outfile:
       json.dump(cluster, outfile, cls=NpEncoder)
profile
to be enterprising

0개의 댓글