QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2160|回复: 0
打印 上一主题 下一主题

删除文件注释

[复制链接]
字体大小: 正常 放大

1186

主题

4

听众

2923

积分

该用户从未签到

跳转到指定楼层
1#
发表于 2024-3-31 17:15 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
输入文件名,行注释标签,块注释标签,生成删除注释后的文件保存并覆盖原文件。% s% T: g" x+ p% U2 X3 T
例如C语言使用 // 和 /* */ 来注释,调用方式如下:
  1. del_comm("main.c","//",["/*","*/"]), T# ]. c6 Q\" L% d
复制代码
  1. # 删除所有注释
    2 v/ G) A0 a! r# f2 Y, Y
  2. def del_comm(file:str,line_comm:str,blok_comm:list[str]):  m. t4 I8 t( Z9 B4 d/ W5 {
  3.     text=""0 ]* V) R) A\" a
  4.     try:% k- q1 r: b0 c3 r
  5.         with open(file,encoding="utf-8") as f:
    - e, q/ V$ Q\" p9 F8 }
  6.             lines=f.readlines()5 N- x2 g, m/ ]4 @- X3 P4 R
  7.     except Exception as e:8 v* |; J1 G' Z; C5 ?
  8.         print("decode failed",file)5 i\" o& B% ^$ b5 _\" L
  9.         return\" ]* K! j5 l# H
  10.     for i in range(len(lines)):
    ( w5 P8 r7 Z\" I0 b$ Q( T5 x; y0 l
  11.         index=lines[i].find(line_comm)
    3 h! G. M8 z5 e. o0 b\" h9 v; q
  12.         if(index>=0):  Y8 R! R4 V0 a; @5 n& e3 C6 F
  13.             lstr=lines[i][:index]
    . c3 ]8 |# u3 S( B/ E9 L+ K
  14.         else:
    ' |! N. J; K& X, l
  15.             lstr=lines[i].rstrip()
    3 _- w  \& a6 V9 R7 M
  16.         if(len(lstr.strip())>0):
    / H1 U4 \# g8 m% G8 k! v2 L- Y
  17.             text+=lstr+'\n'
    , ~8 M0 T8 s. `- _  ~7 ^2 ?1 A
  18.         elif(text[-2:]=='\\\n'):5 ~) ~8 G8 p0 q6 ?! C8 ~* F
  19.             text+='\n'
    4 D* a- l. b6 `$ c/ q) u
  20.     index_start=0
    5 R9 U. U3 M. k
  21.     text_out=""\" Y. `* g$ R7 l3 C8 L
  22.     while True:  l# P1 w) e9 X* H
  23.         index=text.find(blok_comm[0],index_start)+ ^0 y: |- o# N# t- ^
  24.         index_end=text.find(blok_comm[1],index)
    9 k- _. e+ G1 e8 f8 t( \
  25.         if(index>=0 and index_end>index):
    $ k+ `. s) S, ~. Z
  26.             text_out+= text[index_start:index]
    + S0 b$ S0 C( _5 u6 s
  27.             index_start=index_end+len(blok_comm[1]); ^  a\" g! F% s2 o
  28.         else:' Z* i5 h\" ]8 B( V5 j6 D/ r6 \
  29.             text_out+=text[index_start:]$ I/ Q6 x9 g( i/ p2 O7 S; h1 ?
  30.             break! L* y* Z6 Y0 f\" `0 F5 i' z& }/ G# H! F
  31.     with open(file,mode="w+",encoding="utf-8") as f:
    ' O4 [) f# z) O; N( {
  32.         f.write(text_out). e, J\" Q7 p6 S4 U  T% H: J. o. T% e) z
  33.         . u' ^/ M  x. ?  |/ f
复制代码

6 ^0 m4 A0 E3 Q
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2026-4-19 07:52 , Processed in 0.389662 second(s), 51 queries .

回顶部