- 在线时间
- 479 小时
- 最后登录
- 2026-4-17
- 注册时间
- 2023-7-11
- 听众数
- 4
- 收听数
- 0
- 能力
- 0 分
- 体力
- 7790 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 2923
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1171
- 主题
- 1186
- 精华
- 0
- 分享
- 0
- 好友
- 1
该用户从未签到
 |
输入文件名,行注释标签,块注释标签,生成删除注释后的文件保存并覆盖原文件。
4 v- w# D; D: D, u7 S例如C语言使用 // 和 /* */ 来注释,调用方式如下:- del_comm("main.c","//",["/*","*/"])\" N\" I' e) \; H3 r1 M\" J
复制代码- # 删除所有注释, W: ^3 d& b! c' S4 L+ D\" N
- def del_comm(file:str,line_comm:str,blok_comm:list[str]):
, d3 C- ~3 N7 r- g0 W% J5 a N - text=""
; f$ b& c7 U- O3 L\" e/ t/ Y - try:8 R1 m [: B7 e1 O. k2 p
- with open(file,encoding="utf-8") as f:
: a: m$ n\" j! `, q/ @: I8 { - lines=f.readlines()
. ~2 R! o( a+ L; Y3 k: T' k$ P+ N - except Exception as e:
- O5 M P* y# w4 `7 n1 V4 k. o - print("decode failed",file)# e\" u3 X2 T( g
- return- k' b/ ^8 s5 f5 m4 Y& ~0 r
- for i in range(len(lines)):8 Y! H. Q' i3 a\" K
- index=lines[i].find(line_comm)/ g, m! |5 v- c& I! B, @! G0 U* F
- if(index>=0):& v/ p0 o\" T$ n
- lstr=lines[i][:index]
/ L# Y$ E7 V1 H- X\" W - else:2 L/ K1 S: v2 Y0 ]# M
- lstr=lines[i].rstrip(): a' K- T3 X4 G3 x. I
- if(len(lstr.strip())>0):
$ K) y' n+ c. J8 `# E; X$ P- B - text+=lstr+'\n'1 D0 g6 \0 X$ [; M, u8 b: C
- elif(text[-2:]=='\\\n'):
5 {. X& E' m5 W - text+='\n'8 C7 R. l s1 Q: o% C: G/ ^& M$ }
- index_start=0
& f9 A; i- V/ g# ~5 t - text_out=""
% D* j0 J C\" b% e0 q4 g; d - while True:) e\" C) C `. l+ k |
- index=text.find(blok_comm[0],index_start)
. A' r. t( ^$ o\" j+ [- a6 _ - index_end=text.find(blok_comm[1],index) T7 y\" k2 P9 o
- if(index>=0 and index_end>index):
) h0 w1 e8 K2 C - text_out+= text[index_start:index]
+ L/ ?% X2 {5 M0 d - index_start=index_end+len(blok_comm[1]) k6 t; I2 M3 m8 o; ?
- else:6 t X; c( b2 V2 z/ C
- text_out+=text[index_start:]
2 @, _; a1 W0 ?8 g2 \4 k2 a - break# w/ X0 G0 V\" i' o6 d1 E
- with open(file,mode="w+",encoding="utf-8") as f:
, C0 F\" V5 U2 o z% S* l - f.write(text_out)
\" a$ Y* @ L( M5 l2 G1 n) ?; C - 0 G6 G8 L0 O; ?
复制代码 ; |7 p9 V6 D3 q; _) o9 T. Y
|
zan
|