How to separate python code and comments_python

不言
Release: 2018-04-21 15:58:22
Original
1908 people have browsed it

Below I will share with you a method of separating python code and comments. It has a good reference value and I hope it will be helpful to everyone. Let's take a look together

Python's comment method is different from C language, C, and Java

In python language, use '#' to comment, followed by three quotation marks To make comments

The program of this article will separate the comments using '#' and three quotation marks in python. Of course, it can also be merged back again

For those who need it Come and watch

#!/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()
Copy after login

Known problems with this program are that it cannot handle comments starting with '#' after spaces, and all comment lines. It must be written in the top case

If you have time in the future, I will write a complete version again

Related recommendations:

Python solves all problems for a given string Method to determine whether a subsequence is a palindrome sequence

The above is the detailed content of How to separate python code and comments_python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!