- 在线时间
- 480 小时
- 最后登录
- 2026-6-1
- 注册时间
- 2023-7-11
- 听众数
- 4
- 收听数
- 0
- 能力
- 0 分
- 体力
- 7823 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 2934
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1174
- 主题
- 1189
- 精华
- 0
- 分享
- 0
- 好友
- 1
该用户从未签到
 |
输入文件名,行注释标签,块注释标签,生成删除注释后的文件保存并覆盖原文件。7 U( N R% [! Y; Z2 R" m" C1 e+ I* W
例如C语言使用 // 和 /* */ 来注释,调用方式如下:- del_comm("main.c","//",["/*","*/"])
\" _) T5 f% @: w6 u8 y7 s# a
复制代码- # 删除所有注释
) a2 O' s- J& s8 W8 ?( l5 z% m - def del_comm(file:str,line_comm:str,blok_comm:list[str]):( f6 X* R; r% W ~% y
- text=""
# B. V% }( P) Z8 ^ - try:
, I* s# A+ w& |+ O0 T7 b - with open(file,encoding="utf-8") as f:+ Y+ M' y1 I/ J
- lines=f.readlines()- \4 u/ Y# @5 s2 c5 u' C) q
- except Exception as e:4 }1 A# A: M* R& x
- print("decode failed",file)
* n+ _# [+ h9 S, S7 e! R - return; D2 b9 `; V1 b0 G/ S/ W\" h
- for i in range(len(lines)):
6 @1 D4 U& | v N3 q2 s* p- t - index=lines[i].find(line_comm)
9 ]4 \0 r' S% T1 _1 w' \ - if(index>=0):
0 g4 {+ ` `. G - lstr=lines[i][:index]8 O! D2 R, Q2 A6 ]
- else:
, t) D6 @# ~1 k# C L( w( o - lstr=lines[i].rstrip()
! c# o' y' l5 o/ D7 F$ ]4 h - if(len(lstr.strip())>0):
; f1 {+ n w5 U- P0 C' P\" e - text+=lstr+'\n'
3 A; H6 m+ N' x$ F b# l1 v) H6 k - elif(text[-2:]=='\\\n'):* ~3 n+ j8 R4 B, J. X. }
- text+='\n'
5 u/ V# E$ N) ~2 N6 G$ X' P$ h - index_start=0
7 q\" H, V: U% D3 D - text_out=""4 P) G5 w# q& q2 d' G k
- while True:
0 ]( Q% g7 J; ^# \ - index=text.find(blok_comm[0],index_start)# u0 Q4 C8 X& v# F& Z# ^
- index_end=text.find(blok_comm[1],index)4 y: ]% C( S) ], o, X& j
- if(index>=0 and index_end>index):% h/ w/ J, J( N, W6 u% ]: j( \3 ?
- text_out+= text[index_start:index]0 v; }\" d' O5 @/ y) f3 q
- index_start=index_end+len(blok_comm[1])
7 {, A7 l' |2 }3 I - else:
; x8 }' [5 a9 i* ^7 m9 s( P - text_out+=text[index_start:]
, p\" s7 j$ W9 X; f - break* v2 q! L* {% c9 o3 b2 U0 X
- with open(file,mode="w+",encoding="utf-8") as f:1 K, a/ ^& z8 B: t8 }, e8 F! }, B
- f.write(text_out)3 ]5 m8 q6 A1 E* \9 a& b1 j
- 9 f2 i0 ~- j5 ~+ z
复制代码
3 w) ?) H# t/ ^+ o( z |
zan
|