- 在线时间
- 480 小时
- 最后登录
- 2026-6-1
- 注册时间
- 2023-7-11
- 听众数
- 4
- 收听数
- 0
- 能力
- 0 分
- 体力
- 7823 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 2934
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1174
- 主题
- 1189
- 精华
- 0
- 分享
- 0
- 好友
- 1
该用户从未签到
 |
输入文件名,行注释标签,块注释标签,生成删除注释后的文件保存并覆盖原文件。! q+ b* l8 h4 j
例如C语言使用 // 和 /* */ 来注释,调用方式如下:- del_comm("main.c","//",["/*","*/"]) o& B2 m' b4 O8 J3 U5 H. J$ R
复制代码- # 删除所有注释
! {2 }2 A: f4 Z( m% g* s - def del_comm(file:str,line_comm:str,blok_comm:list[str]):
) x2 W! p# ~0 R6 u - text=""
& Y. s+ o/ Y1 }1 ? O! l$ q: U- G - try:: @# W9 W% k8 ~; |; L
- with open(file,encoding="utf-8") as f:
4 M1 Z& a! Z\" H# B7 J7 \2 r - lines=f.readlines()
0 u3 t7 C$ S) _% k - except Exception as e:
8 z5 b7 D- Q& Y\" x3 I) ]; k8 n4 ` - print("decode failed",file)
! g/ x1 `4 \; M - return
. \) t8 t. R# u* x - for i in range(len(lines)):* `! c( `2 p# g2 |. D$ v
- index=lines[i].find(line_comm)
, t, n: B& a# a4 D' V - if(index>=0):
1 ~# n$ z0 ]5 `5 ~- C - lstr=lines[i][:index]
, N( E8 x9 B$ x! e4 q0 T* U, N - else:\" R: d) Q! ^/ [, x6 {
- lstr=lines[i].rstrip()
6 n2 b. j2 Y6 o: Q/ x - if(len(lstr.strip())>0):6 M# F! M3 x: K6 R7 b$ r
- text+=lstr+'\n'' Q8 @& Z/ i0 P V, z& J+ W
- elif(text[-2:]=='\\\n'):
, h0 h& [# e U# f, M' s* X! V - text+='\n'0 _1 [/ g3 k$ t- _: {8 ?4 L
- index_start=0
/ r4 N) G. ?; e; @ - text_out=""# N6 t5 \ F- F+ y8 r
- while True:4 _5 g; Q& }3 p; e! o1 g
- index=text.find(blok_comm[0],index_start)
\" e8 k2 e- i4 t4 _\" e) T - index_end=text.find(blok_comm[1],index)* ?4 n2 L) P) ]& U
- if(index>=0 and index_end>index):
+ h, i* F\" m. E% K - text_out+= text[index_start:index]
. c; [4 X* R0 Q\" G - index_start=index_end+len(blok_comm[1])* {. A- W9 Q+ i3 i
- else:. N4 \$ S( `# E
- text_out+=text[index_start:]
- k5 }' _2 ? R\" ^\" i - break+ X\" ]* H8 r2 R\" B5 F9 o3 W
- with open(file,mode="w+",encoding="utf-8") as f:
: s, }8 ~9 y) E - f.write(text_out)$ n$ B4 ~3 Z. w2 q* p. q
-
! O* t) @% v0 |. x5 C4 i# B
复制代码 ( l) g& j# h- O, U8 ~" C
|
zan
|