- 在线时间
- 480 小时
- 最后登录
- 2026-6-1
- 注册时间
- 2023-7-11
- 听众数
- 4
- 收听数
- 0
- 能力
- 0 分
- 体力
- 7823 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 2934
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1174
- 主题
- 1189
- 精华
- 0
- 分享
- 0
- 好友
- 1
该用户从未签到
 |
输入文件名,行注释标签,块注释标签,生成删除注释后的文件保存并覆盖原文件。
" h$ P' A' s6 C* A! W+ q例如C语言使用 // 和 /* */ 来注释,调用方式如下:- del_comm("main.c","//",["/*","*/"])) V2 [1 ~, M+ A% G0 T) {% E$ N2 i3 u
复制代码- # 删除所有注释
9 H% t0 f! e5 B6 k! m& y; N - def del_comm(file:str,line_comm:str,blok_comm:list[str]):
9 w0 K' P. i2 z7 o2 i( | - text=""
. {7 Z9 {\" l2 w5 s$ M/ }$ X - try:8 F/ W& g |\" y- u0 Z4 D
- with open(file,encoding="utf-8") as f:0 j& y% i/ b1 k: y5 c6 ?, h* ]; l
- lines=f.readlines()3 p. A2 s: N\" u\" N8 v7 U
- except Exception as e:
9 _* Y6 j: A1 n: X - print("decode failed",file)
. E) o5 |3 C# @2 l3 t - return
( Z7 j9 S\" Z% w7 i/ S1 g - for i in range(len(lines)):
( x\" ~' ^6 ?$ V4 S% p4 H - index=lines[i].find(line_comm)
; J* l7 d! C- ]: l; i - if(index>=0):
. l* Y' b* ^5 _- R9 x2 q7 ~ W - lstr=lines[i][:index]& d2 h; j3 x4 ^5 J- c3 G
- else:& w \- Z( [$ s# I
- lstr=lines[i].rstrip()
: v0 x& X$ _9 W, n) B: K4 R& F - if(len(lstr.strip())>0):
r. p( H' p3 j S - text+=lstr+'\n'2 ?8 @- A- J- a! l% B7 c
- elif(text[-2:]=='\\\n'):
0 H, r- f\" G' l' R6 t7 Q3 ~ - text+='\n'
7 s* J: P1 h3 P2 ~ - index_start=0
* @' p1 U5 w. v1 c - text_out="" Y5 U! C- \! v\" n/ i
- while True:
% Y! c, j8 R. H - index=text.find(blok_comm[0],index_start)1 K7 u% R! p2 z* l- f1 q
- index_end=text.find(blok_comm[1],index)7 ]) G5 f E\" ~
- if(index>=0 and index_end>index):
6 k6 G- M% a: l - text_out+= text[index_start:index]9 A+ E4 g. o. }% i' n
- index_start=index_end+len(blok_comm[1])) U3 \: a1 A4 v\" ]
- else:: Y- V5 L( a& P\" O! i( l
- text_out+=text[index_start:]
$ B1 I. i4 O Z- D - break
4 R) l) Q0 U4 X% o& n - with open(file,mode="w+",encoding="utf-8") as f:5 u/ m4 a6 z$ [. D4 y' ^ U9 P
- f.write(text_out)2 K: q6 v6 h. c\" d: z
- 6 k0 g8 L\" l _0 a1 r$ T
复制代码
) ^9 @9 C. }( n5 s |
zan
|