将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使用心得之获得github代码库列表
Jun 25 Python
python各种语言间时间的转化实现代码
Mar 23 Python
python+selenium实现163邮箱自动登陆的方法
Dec 31 Python
python pandas实现excel转为html格式的方法
Oct 23 Python
pyshp创建shp点文件的方法
Dec 31 Python
PyQT5 QTableView显示绑定数据的实例详解
Jun 25 Python
django 连接数据库 sqlite的例子
Aug 14 Python
opencv python如何实现图像二值化
Feb 03 Python
python 代码实现k-means聚类分析的思路(不使用现成聚类库)
Jun 01 Python
python GUI计算器的实现
Oct 09 Python
Python txt文件如何转换成字典
Nov 03 Python
flask框架中的cookie和session使用
Jan 31 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
PHP 遍历XP文件夹下所有文件
2008/11/27 PHP
PHP的mysqli_query参数MYSQLI_STORE_RESULT和MYSQLI_USE_RESULT的区别
2014/09/29 PHP
腾讯微博提示missing parameter errorcode 102 错误的解决方法
2014/12/22 PHP
PHP实现Javascript中的escape及unescape函数代码分享
2015/02/10 PHP
php通过strpos查找字符串出现位置的方法
2015/03/17 PHP
php 删除指定文件夹的实例讲解
2017/07/25 PHP
30个精美的jQuery幻灯片效果插件和教程
2011/08/23 Javascript
js实现全屏漂浮广告移入光标停止移动
2013/12/02 Javascript
运用JQuery的toggle实现网页加载完成自动弹窗
2014/03/18 Javascript
简介JavaScript中的getUTCFullYear()方法的使用
2015/06/10 Javascript
jquery pagination插件动态分页实例(Bootstrap分页)
2016/12/23 Javascript
js实现悬浮窗效果(支持拖动)
2017/03/09 Javascript
js判断是否是手机页面
2017/03/17 Javascript
vueJs实现DOM加载完之后自动下拉到底部的实例代码
2018/08/31 Javascript
vue 利用路由守卫判断是否登录的方法
2018/09/29 Javascript
vue实现跨域的方法分析
2019/05/21 Javascript
vue input标签通用指令校验的实现
2019/11/05 Javascript
JavaScript 中的无穷数(Infinity)详解
2020/02/13 Javascript
layui实现显示数据表格、搜索和修改功能示例
2020/06/03 Javascript
用pywin32实现windows模拟鼠标及键盘动作
2014/04/22 Python
使用PyCharm配合部署Python的Django框架的配置纪实
2015/11/19 Python
详解python statistics模块及函数用法
2019/10/27 Python
python列表推导式操作解析
2019/11/26 Python
python 浅谈serial与stm32通信的编码问题
2019/12/18 Python
Python模块的定义,模块的导入,__name__用法实例分析
2020/01/07 Python
pycharm的python_stubs问题
2020/04/08 Python
css3圆角边框和边框阴影示例
2014/05/05 HTML / CSS
普天C++笔试题
2016/03/20 面试题
经典c++面试题四
2015/05/14 面试题
《画》教学反思
2014/04/14 职场文书
优秀教师感人事迹材料
2014/05/04 职场文书
2014年科室工作总结范文
2014/12/19 职场文书
同乡会致辞
2015/07/30 职场文书
2015年乡镇食品安全工作总结
2015/10/22 职场文书
python获取淘宝服务器时间的代码示例
2021/04/22 Python
分享7个 Python 实战项目练习
2022/03/03 Python