调整注释内容在代码的上一行
我们经常可以看到有些公开的项目喜欢将注释内容写在同一行,特别是部分内容较长不方便阅读,为此写了一个脚本进行简单的批量处理。
import os
def split_code_comment(src_file_path, dst_file_path):
f = open(src_file_path, 'r', encoding='utf-8')
ff = open(dst_file_path, 'w', encoding='utf-8')
with f,ff:
new_lines = []
lines = f.readlines()
for line in lines:
if line.strip().startswith('#'):
new_lines.append(line)
elif '#' in line:
line_code, line_comment = line.split("#")
null_str_num = len(line_code) - len(line_code.lstrip())
new_lines.append(
"".join(
(
null_str_num * " ", "# ",
line_comment.rstrip(), "\n"
)
)
)
new_lines.append(line_code.rstrip() + "\n")
else:
new_lines.append(line)
ff.writelines(new_lines)