From 4d8b6c811e7eb7f9526a63a5097d6e349caee873 Mon Sep 17 00:00:00 2001 From: schrmh <32062480+schrmh@users.noreply.github.com> Date: Sun, 30 Jul 2023 13:21:43 +0200 Subject: [PATCH] Fix UnicodeEncodeError: 'ascii' codec can't encode character CJK characters, umlauts and Unicode as well as Unicode PUA symbols don't cause a problem anymore. Fixes #32 #33 #34 #35 --- msbt.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/msbt.py b/msbt.py index 58f5ab5..063d7ce 100755 --- a/msbt.py +++ b/msbt.py @@ -1,5 +1,6 @@ #!/usr/bin/python import argparse +import codecs import json import os.path import re @@ -186,7 +187,9 @@ def to_json(self, filename): output['structure']['LBL1']['lists'] = self.sections['LBL1']['data'] - json.dump(output, open(filename, 'w'), indent=2, sort_keys=True, ensure_ascii=False) + json_str = json.dumps(output, indent=2, sort_keys=True, ensure_ascii=False) + with codecs.open(filename, 'w', encoding='utf-8') as file: + file.write(json_str) def from_json(self, filename): json_data = json.load(open(filename, 'r'))