- 在线时间
- 484 小时
- 最后登录
- 2026-11-10
- 注册时间
- 2023-7-11
- 听众数
- 4
- 收听数
- 0
- 能力
- 0 分
- 体力
- 7961 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 2980
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1184
- 主题
- 1199
- 精华
- 0
- 分享
- 0
- 好友
- 1
该用户从未签到
 |
输入文件名,行注释标签,块注释标签,生成删除注释后的文件保存并覆盖原文件。
* z$ k! u) k7 _4 U+ }0 c3 \例如C语言使用 // 和 /* */ 来注释,调用方式如下:- del_comm("main.c","//",["/*","*/"])5 X) r8 N5 `. |
复制代码- # 删除所有注释
5 b# `) b8 }3 C2 b& D - def del_comm(file:str,line_comm:str,blok_comm:list[str]):
' X5 O& s/ `( F4 A* {3 @ - text=""0 u; ~3 P6 D5 ?1 I3 k4 @0 R
- try:2 P: K1 R' C3 c2 c T
- with open(file,encoding="utf-8") as f:
% V3 O/ x0 |* O# ^9 } - lines=f.readlines()1 F8 S+ @( j\" G q( x
- except Exception as e:
! S8 G9 A$ f4 x$ \. W4 C; z - print("decode failed",file)$ u0 r7 J2 D# O& y
- return( A( F9 I1 l( s4 w! Y
- for i in range(len(lines)):9 Z9 W: Z. U( f' j! v7 `
- index=lines[i].find(line_comm)
1 f# l0 s* _( C/ w+ |8 _0 H5 k - if(index>=0):
' x/ |: O* U s; Y - lstr=lines[i][:index]7 M3 B8 T8 H- P# A- o( b
- else:
6 g% b) P, Y1 {7 x - lstr=lines[i].rstrip()
0 j/ d( O' e# Z n8 ~ - if(len(lstr.strip())>0):8 c2 {6 H4 ~% g
- text+=lstr+'\n'
\" H* g$ w' g B# x - elif(text[-2:]=='\\\n'):
+ L( y- H3 k4 G7 [5 n& [ s - text+='\n'
- g6 a8 R8 A( X9 J7 C; w, L - index_start=0
5 u3 Q! {9 _' |9 G4 |3 I - text_out="": o* I\" g6 N. G9 I
- while True:
8 ` n, L! N- T P, L - index=text.find(blok_comm[0],index_start)( D) u/ W\" S% N _1 ^7 v+ E0 q
- index_end=text.find(blok_comm[1],index)! ]: Y* ~; t( o9 D/ e
- if(index>=0 and index_end>index):1 _, U( v; v1 I5 v8 @
- text_out+= text[index_start:index]8 g9 s' A# r# z; Y2 b ]
- index_start=index_end+len(blok_comm[1]) V z4 s7 O+ M8 ]0 |! \/ @+ u
- else:3 q; I b/ N& {4 b3 }4 I2 b4 m4 _
- text_out+=text[index_start:]( k: ]9 K# X+ B% f
- break9 @8 @: B* y2 J* Z* D; U; ?0 s4 \) ?
- with open(file,mode="w+",encoding="utf-8") as f:
( T d) R+ r3 t. \- R4 U* ?9 P' u3 K - f.write(text_out)
: I( O/ |1 F( H2 ]1 \! E5 T, \ -
2 l- K& h5 z$ @/ s- S
复制代码
; E. |8 q' D3 N) V% x |
zan
|