- 在线时间
- 479 小时
- 最后登录
- 2026-4-17
- 注册时间
- 2023-7-11
- 听众数
- 4
- 收听数
- 0
- 能力
- 0 分
- 体力
- 7790 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 2923
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1171
- 主题
- 1186
- 精华
- 0
- 分享
- 0
- 好友
- 1
该用户从未签到
 |
输入文件名,行注释标签,块注释标签,生成删除注释后的文件保存并覆盖原文件。. U( U; n$ K4 c- O/ `9 h9 q
例如C语言使用 // 和 /* */ 来注释,调用方式如下:- del_comm("main.c","//",["/*","*/"])
( j7 z0 W* ?) g, a3 H/ y# b
复制代码- # 删除所有注释+ d, ~ B4 I c
- def del_comm(file:str,line_comm:str,blok_comm:list[str]):5 i9 x0 _) o1 f& L5 Y
- text=""
8 { o ?\" Y% s/ ]5 Y7 s - try:4 Z$ P. g- ^1 c* W# U$ i
- with open(file,encoding="utf-8") as f:8 J! b) C0 N. c8 c4 `/ o
- lines=f.readlines()
- R% ]9 p1 \9 t8 d! T - except Exception as e:
9 Z4 y- j8 U2 F! [\" O; n, d' i - print("decode failed",file)8 }7 ^+ _7 x' K4 D\" w7 T- n
- return
* [/ T2 a1 ^5 f, g' v - for i in range(len(lines)):
9 H! w, f5 _) o4 ]3 o - index=lines[i].find(line_comm)
) |) B+ [8 J7 z* B! a/ g+ N - if(index>=0):
7 P, K% B7 c! O& F7 b( h4 E - lstr=lines[i][:index]
- W0 w3 M0 b f6 ]: F' S7 f4 p6 C) w - else:) k5 ]# x% M0 `, f$ n; ~% f. [
- lstr=lines[i].rstrip()* w) Z8 Y6 i- @; B- p7 A
- if(len(lstr.strip())>0):* a) W$ \$ p. H+ ?0 l1 T+ S
- text+=lstr+'\n') N: F/ D5 u8 ?; [
- elif(text[-2:]=='\\\n'):! p6 R' J3 t: ?' e
- text+='\n'
& U( [# f8 z' c& [' L2 ?0 Y - index_start=0- M/ W4 E0 \/ H4 F7 _# E
- text_out=""2 d8 A! T' q+ e4 Z7 \2 m1 H2 y# F
- while True:
' @6 B! }* H5 Q$ J+ _6 d7 | - index=text.find(blok_comm[0],index_start)6 `% i- x4 Z+ o6 w! s1 w
- index_end=text.find(blok_comm[1],index)8 [9 h/ e- O0 k* @# G
- if(index>=0 and index_end>index):5 K- \+ b. C, k! a\" }5 s0 H
- text_out+= text[index_start:index]
7 h9 ^% t! a( l5 \ - index_start=index_end+len(blok_comm[1])4 w) m1 ^+ U d2 d8 O/ n b; c
- else:
) d+ m, ~4 W; z- W2 | c - text_out+=text[index_start:]
) r% v; q( Q: g; o. |; \3 [- ~ - break' r. {, |2 v9 f- L% o/ ?6 o
- with open(file,mode="w+",encoding="utf-8") as f:\" r0 c) |! f* W l; J: }) \+ r
- f.write(text_out). `; f9 P8 d: U
- ( V( ~/ H' m! G( N6 W6 Z) R! \/ p# u
复制代码 9 ^4 U4 ]4 {1 \* K- P9 h9 d
|
zan
|