将python代码和注释分离的方法

作者:枫奇 时间:2022-04-06 12:04:50 

python的注释方式和C语言、C++、java有所不同

python语言中,使用‘#' 来进行注释,其次还有使用 三个引号来进行注释

本文的程序将把 python 中 使用‘#' 号 好 三个引号的注释分离出来, 当然也能再次合并回去

有需求的小伙伴可以来围观了


#!/usr/bin/python
#coding=utf-8
import os
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
class Comment_Filter:
#初始化参数
def __init__(self):
self.file=None
self.commentfile=None
self.noncommentline=None
self.resotrefile=None
self.Commentline=[]
self.NonCommentline=[]
self.globalcomment=0
#判断是不是注释行
def is_Comment_Line(self,line,i):
if i > 2 and line.startswith("#"):
return 1
if line.startswith("'''") and self.globalcomment==1:
self.globalcomment=0
return 1
if line.startswith("'''") and self.globalcomment==0:
self.globalcomment=1
return 1
return self.globalcomment
#保存注释行
def save_Comment_Line(self,line,i):
self.Commentline.append({"line":line, "line_num":i})
#保存代码行
def save_NonComment_Line(self,line,i):
self.NonCommentline.append({"line":line, "line_num":i})
#恢复分离的文件
def restore_Org_File(self):
filename="output/"+self.filename+"_org.txt"
self.resotrefile=open(filename, "w+")
for i in range(1,len(self.Commentline)+len(self.NonCommentline)+1):
for commentline in self.Commentline:
if int(commentline['line_num'])==i:
self.resotrefile.write(commentline['line'])
for noncommentline in self.NonCommentline:
if int(noncommentline['line_num'])==i:
self.resotrefile.write(noncommentline['line'])
print "已输出到%s" % filename
self.resotrefile.close()
#主运行函数
def run(self):
if not os.path.exists("output"):
os.mkdir("output")
print "请输入要处理的文件名"
input_file_name=raw_input()
while len(input_file_name)>1:
print "处理文件为%s" % input_file_name
self.file=open(input_file_name)
self.filename=input_file_name.split(".")[1]
commentfilename="output/"+input_file_name.split(".")[1]+"_comment.txt"
self.commentfile=open(commentfilename,"w+")
noncommentlinename="output/"+input_file_name.split(".")[1]+"_code.txt"
self.noncommentline=open(noncommentlinename,"w+")
i = 0
while self.file != None:
line = self.file.readline()
i +=1
if not line:
print "文件已读完"
print "以下是注释内容"
for commentline in self.Commentline:
print "第%d行: %s" % (commentline['line_num'],commentline['line'])
self.commentfile.write(commentline['line'])

print "以下是代码内容"
for noncommentline in self.NonCommentline:
print "第%d行: %s" % (noncommentline['line_num'],noncommentline['line'])
self.noncommentline.write(noncommentline['line'])
restore=raw_input("是否恢复成原文件:")
if restore == 'Y':
self.restore_Org_File()
self.commentfile.close()
self.noncommentline.close()
break
if self.is_Comment_Line(line,i):
self.save_Comment_Line(line,i)
else:
self.save_NonComment_Line(line,i)
print "请输入文件名"
input_file_name=raw_input('if quit,please input Q:')
if input_file_name == 'Q':
break
if __name__ == '__main__':
print '''
*****************************************
**  Welcome to Spider of baidutieba **
**   Created on 2017-05-03     **
**   @author: Jimy _Fengqi     **
*****************************************
'''
my_file_divide_filter=Comment_Filter()
my_file_divide_filter.run()

本程序已知问题, 不能处理 空格之后在以‘#' 开头的注释,所有的注释行,必须是顶格写的

以后有时间的话,再重新写一版完整的吧

来源:https://blog.csdn.net/qiqiyingse/article/details/71131120

标签:python,代码,注释,分离
0
投稿

猜你喜欢

  • GoLang调用链可视化go-callvis使用介绍

    2023-07-16 06:39:59
  • asp.net(c#)实现从sqlserver存取二进制图片的代码

    2023-06-26 21:48:03
  • 在阿里云服务器上配置CentOS+Nginx+Python+Flask环境

    2023-07-26 09:47:46
  • ASP.NET(C#)读取Excel的文件内容

    2023-07-10 22:38:35
  • python 读取yaml文件的两种方法(在unittest中使用)

    2021-04-20 17:38:30
  • 从零开始写jQuery框架

    2008-12-24 13:37:00
  • 五个影响mysql性能的关键配置

    2010-08-08 09:13:00
  • Python3如何在Windows和Linux上打包

    2021-04-24 13:50:50
  • 如何用表单在线建立目录?

    2010-06-16 09:49:00
  • 关于VSCode 配置使用 PyLint 语法检查器的问题

    2023-06-18 17:10:33
  • 介绍Python中的一些高级编程技巧

    2022-09-22 19:23:15
  • python利用Excel读取和存储测试数据完成接口自动化教程

    2022-04-02 05:25:13
  • 利用PHP自动生成印有用户信息的名片

    2023-09-10 22:30:56
  • Go语言区别于其他语言的特性

    2023-06-26 02:52:10
  • mysql与sqlserver的所有区别

    2009-02-27 16:18:00
  • ASP中如何判断字符串中是否包含字母和数字

    2009-07-10 13:12:00
  • asp详解session的用法

    2007-09-07 10:22:00
  • python如何通过psutil获取服务器cpu、内存、磁盘使用率

    2022-11-07 01:01:29
  • Python 选择排序中的树形选择排序

    2023-06-10 04:33:32
  • 如何用python绘制雷达图

    2023-04-19 12:44:09
  • asp之家 网络编程 m.aspxhome.com