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


Posted in Python onApril 21, 2018

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()

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

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

以上这篇将python代码和注释分离的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
在Python的Bottle框架中使用微信API的示例
Apr 23 Python
利用Python代码实现数据可视化的5种方法详解
Mar 25 Python
浅谈Python批处理文件夹中的txt文件
Mar 11 Python
pandas对dataFrame中某一个列的数据进行处理的方法
Jul 08 Python
python实现字符串完美拆分split()的方法
Jul 16 Python
使用Python自动生成HTML的方法示例
Aug 06 Python
python实现京东订单推送到测试环境,提供便利操作示例
Aug 09 Python
python 爬取学信网登录页面的例子
Aug 13 Python
Python和Anaconda和Pycharm安装教程图文详解
Feb 04 Python
Python之关于类变量的两种赋值区别详解
Mar 12 Python
使用keras根据层名称来初始化网络
May 21 Python
Python爬虫HTPP请求方法有哪些
Jun 03 Python
Python基于百度AI的文字识别的示例
Apr 21 #Python
python实现随机调用一个浏览器打开网页
Apr 21 #Python
python爬虫 使用真实浏览器打开网页的两种方法总结
Apr 21 #Python
Python针对给定字符串求解所有子序列是否为回文序列的方法
Apr 21 #Python
Django项目实战之用户头像上传与访问的示例
Apr 21 #Python
基于Python 装饰器装饰类中的方法实例
Apr 21 #Python
使用python装饰器计算函数运行时间的实例
Apr 21 #Python
You might like
smarty 缓存控制前的页面静态化原理
2013/03/15 PHP
apache配置虚拟主机的方法详解
2013/06/17 PHP
PHP抓取、分析国内视频网站的视频信息工具类
2014/04/02 PHP
PHP实现递归无限级分类
2015/10/22 PHP
Yii 2.0自带的验证码使用经验分享
2017/06/19 PHP
实例介绍PHP中zip_open()函数用法
2019/02/15 PHP
PHP实现文字写入图片功能
2019/02/18 PHP
html a标签-超链接中confirm方法使用介绍
2013/01/04 Javascript
Javascript中的默认参数详解
2014/10/22 Javascript
基于jQuery Bar Indicator 插件实现进度条展示效果
2015/09/30 Javascript
jQuery判断浏览器并动态调整select宽度的方法
2016/03/02 Javascript
基于vue2的table分页组件实现方法
2017/03/20 Javascript
基于JavaScript实现瀑布流效果
2017/03/29 Javascript
Angular2使用Guard和Resolve进行验证和权限控制
2017/04/24 Javascript
Bootstrap fileinput文件上传组件使用详解
2017/06/06 Javascript
jQuery 改变P标签文本值方法
2018/02/24 jQuery
puppeteer库入门初探
2019/01/09 Javascript
jquery获取input输入框中的值
2019/11/13 jQuery
vue中注册自定义的全局js方法
2019/11/15 Javascript
解决vuex刷新数据消失问题
2020/11/12 Javascript
Python的ORM框架SQLAlchemy入门教程
2014/04/28 Python
Python操作MongoDB详解及实例
2017/05/18 Python
分享几道你可能遇到的python面试题
2017/07/24 Python
对python 生成拼接xml报文的示例详解
2018/12/28 Python
python实现QQ邮箱/163邮箱的邮件发送
2019/01/22 Python
Python使用到第三方库PyMuPDF图片与pdf相互转换
2019/05/03 Python
用python wxpy管理微信公众号并利用微信获取自己的开源数据
2019/07/30 Python
python中return如何写
2020/06/18 Python
html5中使用hotcss.js实现手机端自适配的方法
2020/04/23 HTML / CSS
HTML5 video进入全屏和退出全屏的实现方法
2020/07/28 HTML / CSS
Huda Beauty官方商店:化妆和美容产品
2020/09/05 全球购物
自考生毕业自我鉴定
2013/10/10 职场文书
总经理职责
2013/12/22 职场文书
应届毕业生自荐信
2014/05/28 职场文书
家电创业计划书
2019/08/05 职场文书
导游词之重庆渣滓洞
2020/01/08 职场文书