- 在线时间
- 481 小时
- 最后登录
- 2026-8-25
- 注册时间
- 2023-7-11
- 听众数
- 4
- 收听数
- 0
- 能力
- 0 分
- 体力
- 7859 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 2946
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1177
- 主题
- 1192
- 精华
- 0
- 分享
- 0
- 好友
- 1
该用户从未签到
 |
输入文件名,行注释标签,块注释标签,生成删除注释后的文件保存并覆盖原文件。
) ^5 e2 j& ^9 Z& I例如C语言使用 // 和 /* */ 来注释,调用方式如下:- del_comm("main.c","//",["/*","*/"])6 y1 |) W, J* J# i2 D5 b% I. m
复制代码- # 删除所有注释' D4 I( {& F+ U- x
- def del_comm(file:str,line_comm:str,blok_comm:list[str]):
. e- [$ g) L6 G1 l7 u' N/ I+ C - text=""\" U$ s) v* A# A9 a4 a
- try:
8 J# S7 L0 C0 v+ g$ u: Y - with open(file,encoding="utf-8") as f:
; ~9 u! W4 ^7 T( J- e6 D\" t) I0 d - lines=f.readlines()\" j. c: W! ]( w& v
- except Exception as e:
5 F+ O) K2 n3 f# q+ q, H! G a - print("decode failed",file)
' v6 ~4 ~( q Q4 j+ n - return3 j1 p7 }5 y: J e
- for i in range(len(lines)):: q! e; e$ w& t3 Q: L1 E3 t5 W
- index=lines[i].find(line_comm)
* N( Q% z* ]# {\" h - if(index>=0):
% y/ G7 y _; L - lstr=lines[i][:index]
( P+ H0 J$ s+ {6 n, ^ - else:
. |7 P1 j8 L! |( w7 m& z4 B7 O - lstr=lines[i].rstrip()
7 I5 R( K7 Z5 C& N - if(len(lstr.strip())>0):
! }6 K2 O }/ G7 { - text+=lstr+'\n'
$ }' V4 M1 ~/ ?9 G/ [ - elif(text[-2:]=='\\\n'):6 a6 h\" x- t u0 p5 x3 T$ ]( f% D
- text+='\n'( }# f8 F) ? B! U; R1 X9 W
- index_start=0
9 G- Q' |/ L8 w3 A# S o1 ^ - text_out=""
0 L' W1 |\" d/ E* N* L1 q - while True:- ]. ?7 q& ?\" _# K' N2 B. d/ H
- index=text.find(blok_comm[0],index_start)
( J' z' \7 b' p! y( _0 {7 K. C - index_end=text.find(blok_comm[1],index)
* K4 S8 ^2 q' S. A\" r) g - if(index>=0 and index_end>index):5 p/ {- t/ b\" ^/ a( r; k( Y
- text_out+= text[index_start:index]
8 q4 D\" F) z2 M) T - index_start=index_end+len(blok_comm[1])* i2 v# J/ | {1 n
- else:
) R. ?. T- o9 [( m, p3 a* Y+ P - text_out+=text[index_start:]2 }5 O0 ?9 |3 ?! z- M/ J\" n0 P% Q
- break' i+ ]7 x2 {2 Z1 T' f- I. s2 O
- with open(file,mode="w+",encoding="utf-8") as f:\" o' Y; o3 |1 _7 A; |) _
- f.write(text_out)
; ?; [& N2 x* b) ? - 2 Q6 X+ P/ Y! U7 n) | l5 q
复制代码
5 |4 p+ q: o5 |) C9 ^ t |
zan
|