-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonProcess.py
More file actions
111 lines (76 loc) · 3.17 KB
/
Copy pathJsonProcess.py
File metadata and controls
111 lines (76 loc) · 3.17 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# coding=gbk
import json
from stack import MyStack
import time
from GeneralTool import GeneralTool
_PATH = "./output/process/"
class JsonProcess:
"""
main process part!
"""
_filepath: str
_resetkey: str
_numstart: int
_stack: MyStack
def __init__(self, inputpath):
self._filepath = inputpath
self._stack = MyStack()
def processId(self, num):
self._numstart = int(num)
print("[操作]读取源文件...")
print('')
time.sleep(1)
datalist = GeneralTool.parse_json(self._filepath) #List[Dict]
print("[操作]读取完成")
print('')
if datalist[0] == False:
print("这个文件是空的!操作终止...")
print("[操作]没有任何输出")
print('')
return
time.sleep(1)
print("[操作]修改数据...")
print('')
data = datalist[1]
try:
for i in data:
oldid = i["id"]
print(oldid)
self._stack.push(oldid) # push to stack
for key, value in i.items():
strint = str(self._numstart)
if key == "id":
if self._stack.checkIn(value):
self._numstart -= 1
i[key] = int(str(self._numstart) + str(self._stack.checkGap(value)))
oldid = oldid // 10
else:
i[key] = self._numstart
print("id " + str(oldid) + " ---> " + str(i[key]))
# change id part
elif isinstance(value, str):
if str(oldid) in value:
before = value
temp = value[:-len(str(oldid))]
i[key] = temp + strint
print(key + ": " + before + " ---> " + i[key])
#change complex value part such as drama_dialogue90003014
elif value.isdigit() and int(value) == oldid + 1:
before = value
i[key] = str(self._numstart + 1)
print(key + ": " + before + " ---> " + i[key])
#change nextdialogue
self._numstart += 1
print("[操作]修改完成")
print('')
except Exception as e:
print("[失败]\t\t" + e)
print("[操作]开始写入新文件...")
time.sleep(1)
try:
with open("./output/process/" + self._filepath, "w") as outfile:
outfile.write(json.dumps(data, indent = 2, ensure_ascii = False))
print("[操作]写入完成")
except Exception as e:
print("[失败]\t\t" + e)
return