- 在线时间
- 479 小时
- 最后登录
- 2026-4-17
- 注册时间
- 2023-7-11
- 听众数
- 4
- 收听数
- 0
- 能力
- 0 分
- 体力
- 7790 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 2923
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1171
- 主题
- 1186
- 精华
- 0
- 分享
- 0
- 好友
- 1
该用户从未签到
 |
输入文件名,行注释标签,块注释标签,生成删除注释后的文件保存并覆盖原文件。
# x' R7 B5 O5 K& w例如C语言使用 // 和 /* */ 来注释,调用方式如下:- del_comm("main.c","//",["/*","*/"])
) C# k3 V8 D: i9 e0 Q
复制代码- # 删除所有注释
& O+ l4 [) c4 F, [; H3 K: Y- ~( { - def del_comm(file:str,line_comm:str,blok_comm:list[str]):2 g5 |+ T) w4 y& ^
- text=""
1 C- n8 V0 C+ U F' i) p6 z/ f - try:0 {! n: M3 j; p% D
- with open(file,encoding="utf-8") as f:
7 \( k- A$ s4 w1 E$ x, Q, \8 [3 ] - lines=f.readlines(), o4 \: u8 z- X9 [6 r1 ]% e
- except Exception as e:
' x2 m: n: }5 f\" v - print("decode failed",file)2 j7 N) Q' T& A
- return
, Q2 c$ B0 M% G3 e; Y4 t - for i in range(len(lines)):6 P5 L; G$ ~6 N( N, c4 P, |
- index=lines[i].find(line_comm)
+ Z+ m9 k) s4 R' V - if(index>=0):( q( L\" f7 H* V; F
- lstr=lines[i][:index]
9 g0 {1 Z6 I: F# m - else:0 d% M, @6 f1 m+ t: B1 y
- lstr=lines[i].rstrip()5 }' Q8 R5 D6 a3 k* A
- if(len(lstr.strip())>0):* C/ A2 O- t1 |6 c6 \/ j
- text+=lstr+'\n'( J- Y% I+ S7 B1 N7 H+ {+ c
- elif(text[-2:]=='\\\n'):
$ y4 V8 ~\" Y7 R8 I\" D$ D - text+='\n'
. x# B, k! _( p% o9 q - index_start=0* i0 y% L1 W- v, m5 ?
- text_out=""* T. h' j% l, I
- while True:
\" C. I* f/ [\" M: Z% y/ y - index=text.find(blok_comm[0],index_start)
1 i% b0 l! t0 H w1 Q7 f - index_end=text.find(blok_comm[1],index)
; @# d% o& f J - if(index>=0 and index_end>index):
- m r$ ~7 k5 W; a1 q - text_out+= text[index_start:index]
: L$ F# y* b& b- @( O& s - index_start=index_end+len(blok_comm[1])3 V m; u, d9 S
- else:
\" s1 @4 i/ @& r: s, w; o( g - text_out+=text[index_start:]0 |$ ]8 S! n% Y- f ]
- break% P& S\" B! W8 L; R
- with open(file,mode="w+",encoding="utf-8") as f:
9 e* e( k/ m4 J% I3 I; u9 ] - f.write(text_out). L; ~7 u! {7 C( u3 A& `% X
- 6 P* j/ c7 {/ w8 u\" ^
复制代码
; c- L! `9 D; p: L# B |
zan
|