登录  /  注册

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

不言
发布: 2018-04-21 15:58:22
原创
1707人浏览过

下面为大家分享一篇将python代码和注释分离的方法,具有很好的参考价值,希望对大家有所帮助。一起过来看看吧

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_pide_filter=Comment_Filter()
	my_file_pide_filter.run()
登录后复制

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

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

相关推荐:

Python针对给定字符串求解所有子序列是否为回文序列的方法

以上就是将python代码和注释分离的方法_python的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号