教程集 www.jiaochengji.com
教程集 >  Python编程  >  Python入门  >  正文 利用Python脚本过滤文件中的注释

利用Python脚本过滤文件中的注释

发布时间:2021-12-29   编辑:jiaochengji.com
教程集为您提供利用Python脚本过滤文件中的注释等资源,欢迎您收藏本站,我们将为您提供最新的利用Python脚本过滤文件中的注释资源

确保对模块, 函数, 方法和行内注释使用正确的风格,Python中的注释有单行注释和多行注释。如果希望去除文件中所有注释,如何做呢?

Python中的注释:

Python中单行注释以 # 开头,例如::

<pre class="brush:php;toolbar:false"># 这是一个注释 print("Hello, World!")</pre>

多行注释用三个单引号 ''' 或者三个双引号 """ 将注释括起来,例如:

<pre class="brush:php;toolbar:false">#!/usr/bin/python3  ''' 这是多行注释,用三个单引号 这是多行注释,用三个单引号  这是多行注释,用三个单引号 ''' print("Hello, World!")</pre>



使用Python脚本快速去除文件中的注释:

<pre class="brush:php;toolbar:false">#!/usr/bin/python  # -*- coding: GBK -*-  #writer:xmnathan  #py文件去注释  import re  import os  import ConfigParser  Python='CleanNote' def ReadIni(path,section,option):#文件路径,章节,关键词    #读取ini   cf=ConfigParser.ConfigParser()    cf.read(path)    value=cf.get(section,option)#如果用getint()则直接读取该数据类型为整数    return value  def IsPassLine(strLine):    #是否是可以忽略的行    #可忽略行的正则表达式列表    RegularExpressions=["""/'.*#.*/'""","""/".*#.*/"""",              """/'/'/'.*#.*/'/'/'""","""/"/"/".*#.*/"/"/""""]   for One in RegularExpressions:      zz=re.compile(One)      if re.search(zz,strLine)==None:        continue     else:        return True#有匹配 则忽略      return False def ReadFile(FileName):    #读取并处理文件    fobj=open(FileName,'r')    AllLines=fobj.readlines()    fobj.close()    NewStr=''    LogStr='/n s/n'%(FileName.split('//')[-1])#输出的日志    nline=0   for eachiline in AllLines:      index=eachline.find('#')#获取带注释句‘#'的位置索引      if index==-1 or nline<3 or IsPassLine(eachline):        if eachiline.strip()!='':#排除纯空的行          NewStr=NewStr eachiline      else:        if index!=0:          NewStr=NewStr eachiline[:index] '/n'#截取后面的注释部分          LogStr ="ChangeLine: %s/t%s"%(nline,eachline[index:])      nline =1   return NewStr,LogStr  def MakeCleanFile(SrcPath,DescPath,FileList):    fLog=open(DescPath '//' 'CleanNoteLog.txt','w')    for File in FileList:      curStr,LogStr=ReadFile(SrcPath '//' File)      fNew=open(DescPath '//' File,'w')      fNew=write(curStr)      fNew.close()      fLog.write(LogStr)    fLog.close()  def Main():    #从ini获取源文件夹及目标文件夹路径    IniPath=os.getcwd() '//' PtName '.ini'   SrcPath=ReadIni(IniPath,PyName,'SrcPath')#源文件夹    DescPath=ReadIni(IniPath,PyName,'DescPath')#目的文件夹    #如果目的文件夹不存在,创建之    if not os.path.exists(DescPath):      os.makedirs(DescPath)    FileList=[]    for files in os.walk(SrcPath):      for FileName in files[2]:        if FileName.split('.')[-1]=='py':          FileList.append(FileName)    MakeCleanFile(SrcPath,DescPath,FileList)  if __name__=='__main__':    Main()    print '>>>End<<<'   os.system('pause')</pre>

ps:配置文件CleanNote.ini的格式

<pre class="brush:php;toolbar:false">[CleanNote]  SrcPath=E:/test  DescPath=E:/test/newfiles</pre>

批量去除指定源文件夹中的py文件的注释,并生成拷贝与指定目的文件夹

您可能感兴趣的文章:
利用Python脚本过滤文件中的注释
python是免费的么
linux如何运行python脚本
linux如何打开python
linux中如何使用python
Flask模板过滤器的基础知识
python是一种什么类型的编程语言
php 文件下载安全
Python注释用什么符号
PHP与Python进行数据交互

[关闭]
~ ~