Below for you to share a Python code and comments separate method, has a good reference value, I hope to help you. Come and see it together.
Python notation differs from C language, C + +, Java
In the Python language, use ' # ' to annotate, followed by three quotes
The program in this article will separate the comments in Python with the ' # ' three quotes, and of course you can merge them back again.
There's a need for a little buddy to watch.
#!/usr/bin/python#coding=utf-8import osimport sysreload (SYS) sys.setdefaultencoding (' Utf-8 ') class Comment_Filter:# Initialize parameter Def __init__ (self): self.file=noneself.commentfile=noneself.noncommentline=noneself.resotrefile= Noneself.commentline=[]self. noncommentline=[]self.globalcomment=0# judgment is not a comment line def is_comment_line (self,line,i): If I > 2 and Line.startswith ("#"): return 1if line.startswith ("'") and Self.globalcomment==1:self.globalcomment=0return 1if line.startswith ("" ") and Self.globalcomment==0:self.globalcomment=1return 1return self.globalcomment# Save Comment Line 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 (the 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" output to%s "% Filenameself.resotrefile.close () #主运行函数def Run (self): If not os.path.exists (" Output "): Os.mkdir (" output ") print" Please enter the file name to be processed "input_file_name=raw_input () while Len (input_file_name) >1:print" Processing file%s "% input_file_nameself.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 = 0while self.file! = None:line = Self.file.readline () I +=1if not line:print "The file has been read" print "The following is the comment content" for commentline in self. Commentline:print "%d lines:%s"% (commentline[' line_num '],commentline[' line ') self.commentfile.write (commentline[' Line ']) print "The following is the code content" for Noncommentline in self. Noncommentline:Print "%d lines:%s"% (noncommentline[' line_num '],noncommentline[' line ') self.noncommentline.write (noncommentline[' Line ']) restore=raw_input ("whether to revert to the original file:") if restore = = ' Y ': Self.restore_org_file () Self.commentfile.close () Self.noncommentline.close () Breakif self.is_comment_line (line,i): Self.save_comment_line (line,i) else:self.save_ Noncomment_line (line,i) print "Please enter file name" Input_file_name=raw_input (' if quit,please input Q: ') if input_file_name = = ' Q ': Breakif __name__ = = ' __main__ ':p rint ' ***************************************** * * Welcome to Spider of Baidutieba * * * Created on 2017-05-03 * * * @author: Jimy _fengqi * * ***************************************** "my_file_pid E_filter=comment_filter () My_file_pide_filter.run ()
This program is known to issue, can not handle the space after the "#" in the beginning of the comment, all the comment lines, must be shelf written
If you have time, then write a full version again.