Repository navigation
Expand file tree
/
Copy pathfix_serials.py
More file actions
49 lines (40 loc) Β· 1.61 KB
/
Copy pathfix_serials.py
File metadata and controls
49 lines (40 loc) Β· 1.61 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
# Description: Fixes the serial numbers in the README.md table to be in ascending order starting from 1.
# Usage: python fix_serials.py
# Requirement: Python 3.6+
import re
def fix_serials(file_path):
with open(file_path, "r", encoding="utf-8") as f:
lines = f.readlines()
new_lines = []
in_table_body = False
serial_num = 1
for line in lines:
# Identify the separator line to start re-numbering from the next line
if "|---|---|---|---|---|---|---|" in line:
new_lines.append(line)
in_table_body = True
continue
if in_table_body:
if line.strip().startswith("|"):
# Data row: Replace the content of the first column with the current serial number
match = re.match(r"^\|([^|]+)\|", line)
if match:
new_line = "|" + str(serial_num) + "|" + line[match.end(1) + 1 :]
new_lines.append(new_line)
serial_num += 1
else:
new_lines.append(line)
elif line.strip() == "":
# Keep empty lines within the table body
new_lines.append(line)
else:
# End of table body detected (non-empty line not starting with '|')
in_table_body = False
new_lines.append(line)
else:
new_lines.append(line)
with open(file_path, "w", encoding="utf-8", newline="") as f:
f.writelines(new_lines)
if __name__ == "__main__":
fix_serials("README.md")
print("Serial numbers fixed in README.md")