-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
170 lines (140 loc) · 6.29 KB
/
test.py
File metadata and controls
170 lines (140 loc) · 6.29 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# import re
# pattern = r"<<([a-zA-Z0-9]*)>>"
# pattern_use = r"\[\[([a-zA-Z0-9]*)\]\[.*?\]\]"
# sllj = open('/home/shore/OneDrive/数理逻辑/sllj_back.org', 'r')
# lssx = open('/home/shore/OneDrive/离散数学/Disc_Math.org', 'r')
# sllj_text = sllj.read()
# lssx_text = lssx.read()
# sllj_map = {}
# lssx_map = {}
# i = 1
# with open('mark_map_lssx.txt', 'w') as f:
# for match in re.finditer(pattern, lssx_text):
# lssx_map[match.group(1)] = "MK" + str(i)
# print(match.group(0) + " | " + lssx_map[match.group(1)], file=f)
# i += 1
# i = 1
# with open('mark_map_sllj.txt', 'w') as f:
# for match in re.finditer(pattern, sllj_text):
# sllj_map[match.group(1)] = "MK" + str(i)
# print(match.group(0) + " | " + sllj_map[match.group(1)], file=f)
# i += 1
# for match in sllj_map:
# if match in lssx_map:
# print("double mark set: " + match)
# for match in lssx_map:
# if match in sllj_map:
# print("double mark set: " + match)
# # for each in sllj_map:
# # print(each + " " + sllj_map[each])
# # print("====================================")
# # for each in lssx_map:
# # print(each + " " + lssx_map[each])
# with open('log_lssx.txt', 'w') as f:
# for match in re.finditer(pattern_use, lssx_text):
# if match.group(1) in lssx_map:
# print(lssx_text[match.regs[0][0]:match.regs[0][1]] + " | " + lssx_text[match.regs[1][0]:match.regs[1][1]] + " | " + lssx_map[match.group(1)], file=f)
# elif match.group(1) in sllj_map:
# print(lssx_text[match.regs[0][0]:match.regs[0][1]] + " | " + lssx_text[match.regs[1][0]:match.regs[1][1]] + " | " + "~/OneDrive/数理逻辑/sllj.org::" + sllj_map[match.group(1)], file=f)
# else:
# print(lssx_text[match.regs[0][0]:match.regs[0][1]] + " | " + lssx_text[match.regs[1][0]:match.regs[1][1]] + " | " + "not found")
# exit(1)
# with open('log_sllj.txt', 'w') as f:
# for match in re.finditer(pattern_use, sllj_text):
# if match.group(1) in sllj_map:
# print(sllj_text[match.regs[0][0]:match.regs[0][1]] + " | " + sllj_text[match.regs[1][0]:match.regs[1][1]] + " | " + sllj_map[match.group(1)], file=f)
# elif match.group(1) in lssx_map:
# print(sllj_text[match.regs[0][0]:match.regs[0][1]] + " | " + sllj_text[match.regs[1][0]:match.regs[1][1]] + " | " + "~/OneDrive/离散数学/Disc_Math.org::" + lssx_map[match.group(1)], file=f)
# else:
# print(sllj_text[match.regs[0][0]:match.regs[0][1]] + " | " + sllj_text[match.regs[1][0]:match.regs[1][1]] + " | " + "not found")
# exit(1)
# # Create new strings for modified text
# sllj_text_rep_mark = ""
# lssx_text_rep_mark = ""
# # Replace marks
# start = 0
# for match in re.finditer(pattern, lssx_text):
# lssx_text_rep_mark += lssx_text[start:match.regs[1][0]]
# lssx_text_rep_mark += lssx_map[match.group(1)]
# start = match.regs[1][1]
# lssx_text_rep_mark += lssx_text[start:]
# # Replace marks
# start = 0
# for match in re.finditer(pattern, sllj_text):
# sllj_text_rep_mark += sllj_text[start:match.regs[1][0]]
# sllj_text_rep_mark += sllj_map[match.group(1)]
# start = match.regs[1][1]
# sllj_text_rep_mark += sllj_text[start:]
# sllj_text_rep_link = ""
# lssx_text_rep_link = ""
# # replace links
# start = 0
# for match in re.finditer(pattern_use, lssx_text_rep_mark):
# lssx_text_rep_link += lssx_text_rep_mark[start:match.regs[1][0]]
# if match.group(1) in lssx_map:
# lssx_text_rep_link += lssx_map[match.group(1)]
# elif match.group(1) in sllj_map:
# lssx_text_rep_link += "~/OneDrive/数理逻辑/sllj.org::" + sllj_map[match.group(1)]
# else:
# print("not found")
# exit(1)
# start = match.regs[1][1]
# lssx_text_rep_link += lssx_text_rep_mark[start:]
# # replace links
# start = 0
# for match in re.finditer(pattern_use, sllj_text_rep_mark):
# sllj_text_rep_link += sllj_text_rep_mark[start:match.regs[1][0]]
# if match.group(1) in sllj_map:
# sllj_text_rep_link += sllj_map[match.group(1)]
# elif match.group(1) in lssx_map:
# sllj_text_rep_link += "~/OneDrive/离散数学/lssx.org::" + lssx_map[match.group(1)]
# else:
# print("not found")
# exit(1)
# start = match.regs[1][1]
# sllj_text_rep_link += sllj_text_rep_mark[start:]
# # Write the new text to files
# f_1 = open('sllj.org', 'w')
# f_2 = open('lssx.org', 'w')
# f_1.write(sllj_text_rep_link)
# f_2.write(lssx_text_rep_link)
# k = 32
# for i in range(1, k):
# print(str(i) +":" ,(i+2)/(k/2) - (2*i-1)/((int)(k/i)*i))
def load_map(file_path):
"""
加载文件内容到字典,假设文件中每行的格式为 "<<KEY>>|VALUE".
"""
result_map = {}
with open(file_path, 'r', encoding='utf-8') as file:
for line in file:
if '|' in line:
key, value = line.strip().split('|', 1)
clean_key = key.replace('<<', '').replace('>>', '').replace(' ','') # 移除 << 和 >>
clean_value = value.replace(' ', '') # 移除空格
result_map[clean_key] = clean_value
return result_map
def replace_content_in_file(file_path, lssx_map, sllj_map):
"""
替换文件中特定格式的字段。
"""
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
# 处理 lssx_map 替换
for key, value in lssx_map.items():
search_pattern = f"[[~/文档/note/离散数学/Disc_Math.org::{key}]"
replace_pattern = f"[[~/文档/note/离散数学/lssx.org::{value}]"
content = content.replace(search_pattern, replace_pattern)
# 处理 sllj_map 替换
for key, value in sllj_map.items():
search_pattern = f"[[~/文档/note/离散数学/Disc_Math.org::{key}]"
replace_pattern = f"[[~/文档/note/数理逻辑/sllj.org::{value}]"
content = content.replace(search_pattern, replace_pattern)
# 保存更改后的内容
with open(file_path + ".new", 'w', encoding='utf-8') as file:
file.write(content)
# 加载字典
lssx_map = load_map('mark_map_lssx.txt')
sllj_map = load_map('mark_map_sllj.txt')
# 替换文件中的内容
replace_content_in_file('/home/shore/文档/note/高等代数/Algb-2-Liner_Space.org', lssx_map, sllj_map) # 替换 'your_target_file.txt' 中的内容