- 在线时间
- 481 小时
- 最后登录
- 2026-8-25
- 注册时间
- 2023-7-11
- 听众数
- 4
- 收听数
- 0
- 能力
- 0 分
- 体力
- 7859 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 2946
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1177
- 主题
- 1192
- 精华
- 0
- 分享
- 0
- 好友
- 1
该用户从未签到
 |
输入文件名,行注释标签,块注释标签,生成删除注释后的文件保存并覆盖原文件。3 _: t- t1 A. P! s7 d8 B
例如C语言使用 // 和 /* */ 来注释,调用方式如下:- del_comm("main.c","//",["/*","*/"])% e5 g/ e4 n ?# |0 H
复制代码- # 删除所有注释3 {# `# A, O7 {; s, r
- def del_comm(file:str,line_comm:str,blok_comm:list[str]):
0 i8 L. p$ r) J; b7 C, T8 r- o - text=""
) E4 M, n+ I; [8 J4 Z, Q7 w - try:
% e% Z- ~; ?& s* W# P* M1 E8 F, T - with open(file,encoding="utf-8") as f:% f8 L\" B\" W5 ~7 X
- lines=f.readlines()$ B! g$ I2 n4 e: W# U. w7 m1 s3 u
- except Exception as e:\" ]0 }: Z. L* _5 D6 x, L9 ^\" n
- print("decode failed",file)' P( _& G6 M% s; k! p% `* O8 N! m* t
- return
& e8 x& X# d* d6 v3 G% @ - for i in range(len(lines)):* B3 u( G: B' n- O! H\" m6 G
- index=lines[i].find(line_comm)7 c0 V. J+ h0 M3 _, V) ?* v1 h
- if(index>=0):; ]4 o% r* {3 I
- lstr=lines[i][:index]( x1 l5 t, d8 V2 R' e
- else:
4 L; P5 {* T' E& Z1 y9 ?, d: z& v - lstr=lines[i].rstrip()
/ S1 f; F4 v6 A' O; C& u - if(len(lstr.strip())>0):$ L8 ?/ I) D; [9 {6 k) \
- text+=lstr+'\n'
# T4 K1 S8 K, Q5 H\" K7 k0 v - elif(text[-2:]=='\\\n'):
0 u$ o3 x& l\" B* Y h - text+='\n'
. w: _5 h1 y\" J% D- a2 i# K; c - index_start=04 P* X/ ^& J6 {) S
- text_out=""
, h/ q7 t. c$ `+ w - while True:
- ?! I\" s! b' U& T7 f, _, Q) ` - index=text.find(blok_comm[0],index_start)
! S+ }& d+ o4 L - index_end=text.find(blok_comm[1],index)
8 K9 T5 { R/ a+ J/ z, I - if(index>=0 and index_end>index):( x8 ^/ B+ L, n5 _
- text_out+= text[index_start:index]8 \( e, X- e3 G ?
- index_start=index_end+len(blok_comm[1])2 [: V; V' N4 E6 Q& f
- else:, y8 Y$ O1 [* q2 c' @: D
- text_out+=text[index_start:]/ F, y3 t+ y4 w
- break
- L* N1 G6 {- c U6 t4 Y! L - with open(file,mode="w+",encoding="utf-8") as f:
2 R. I; {9 q! b1 r0 P) o - f.write(text_out)
% }3 R\" W$ L+ n% m% d -
/ O8 g z' K) l8 k- ]! F7 h6 M
复制代码
3 _: g. v1 }" P. H |
zan
|