提交 342a5f05 编写于 作者: F feilong

fix json load encoding bug

上级 6e0668b3
...@@ -15,8 +15,9 @@ formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') ...@@ -15,8 +15,9 @@ formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter) handler.setFormatter(formatter)
logger.addHandler(handler) logger.addHandler(handler)
def load_json(p): def load_json(p):
with open(p, 'r') as f: with open(p, 'r', encoding='utf-8') as f:
return json.loads(f.read()) return json.loads(f.read())
...@@ -100,11 +101,13 @@ class TreeWalker: ...@@ -100,11 +101,13 @@ class TreeWalker:
for index, chapter in enumerate(level_node["children"]): for index, chapter in enumerate(level_node["children"]):
chapter_title = list(chapter.keys())[0] chapter_title = list(chapter.keys())[0]
chapter_node = list(chapter.values())[0] chapter_node = list(chapter.values())[0]
chapter_path = os.path.join(level_path, f"{index+1}.{chapter_title}") chapter_path = os.path.join(
level_path, f"{index+1}.{chapter_title}")
self.load_sections(chapter_path, chapter_node) self.load_sections(chapter_path, chapter_node)
for index, section_node in enumerate(chapter_node["children"]): for index, section_node in enumerate(chapter_node["children"]):
section_title = list(section_node.keys())[0] section_title = list(section_node.keys())[0]
full_path = os.path.join(chapter_path, f"{index}.{section_title}") full_path = os.path.join(
chapter_path, f"{index}.{section_title}")
if os.path.isdir(full_path): if os.path.isdir(full_path):
self.ensure_exercises(full_path) self.ensure_exercises(full_path)
...@@ -230,8 +233,8 @@ class TreeWalker: ...@@ -230,8 +233,8 @@ class TreeWalker:
config = { config = {
"node_id": self.gen_node_id(), "node_id": self.gen_node_id(),
"keywords": [], "keywords": [],
"children":[], "children": [],
"export":[] "export": []
} }
dump_json(config_path, config, exist_ok=True, override=True) dump_json(config_path, config, exist_ok=True, override=True)
else: else:
...@@ -295,4 +298,3 @@ class TreeWalker: ...@@ -295,4 +298,3 @@ class TreeWalker:
if "exercise_id" not in exercise: if "exercise_id" not in exercise:
exercise["exercise_id"] = uuid.uuid4().hex exercise["exercise_id"] = uuid.uuid4().hex
dump_json(full_name, exercise) dump_json(full_name, exercise)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册