-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
33 lines (21 loc) · 924 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import json
def read_json(data_path):
with open(data_path, encoding="utf-8") as fr:
return json.load(fr)
def write_json(data, target_path):
with open(target_path, "w", encoding="utf-8") as fw:
json.dump(data, fw, ensure_ascii=False)
def read_json_lines(data_path):
with open(data_path, 'r', encoding="utf-8") as fr:
return [json.loads(item) for item in fr.readlines()]
def write_json_lines(data, target_path):
with open(target_path, "w", encoding="utf-8") as fw:
for item in data:
fw.write(json.dumps(item, ensure_ascii=False) + "\n")
def append_json_lines(data, target_path):
with open(target_path, "a", encoding="utf-8") as fw:
for item in data:
fw.write(json.dumps(item, ensure_ascii=False) + "\n")
def read_txt(data_path):
with open(data_path, "r", encoding="utf-8") as fr:
return "".join(fr.readlines()).strip()