将python代码和注释分离的方法
作者:枫奇 发布时间:2022-04-06 12:04:50
标签: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_divide_filter=Comment_Filter()
my_file_divide_filter.run()
本程序已知问题, 不能处理 空格之后在以‘#' 开头的注释,所有的注释行,必须是顶格写的
以后有时间的话,再重新写一版完整的吧
来源:https://blog.csdn.net/qiqiyingse/article/details/71131120
0
投稿
猜你喜欢
- 从过往MySQL数据库生产环境的维护工作中,总结的一些小经验和知识,未必有多深奥,但是对我们消除隐患,确保MySQL数据库生产环境四个9的作
- 1.python中列表list的拷贝,会有什么需要注意的呢? python变量名相当于标签名。list2=list1 ,直接赋值,实质上指向
- 使用tf.trian.NewCheckpointReader(model_dir)一个标准的模型文件有一下文件, model_dir就是My
- python继承,python丰富的类因为继承而变得多姿多彩,如果语言不支持继承,那么类就没什么优势。1、首先我们来定义两个类一个dog类,
- 如果在session级保存一个dictionary对象会降低系统的性能,而在application级保存一个dictionary对象会导致w
- PHP mysqli_rollback() 函数关闭自动提交,做一些查询,提交查询,然后回滚当前事务:<?php// 假定数据库用户名
- python修改大数据文件时,如果全加载到内存中,可能会导致内存溢出。因此可借用如下方法,将分件分段读取修改。with open('
- 五、过渡转化的使用在《mind hack》一书中,揭示了人脑鲜为人知的工作原理。其中提到了“突然的移动或闪烁会吸引人的注意力,这正是负责视觉
- 然而有些情况只需要传递几个文件,而且文件体积并不太大,这种情况下使用组件则有点牛刀杀鸡的感觉,通过html自带的<input type
- 项目运行环境阿里云(单核CPU, 1G内存, Ubuntu 14.04 x64 带宽1Mbps), 具体购买和ssh连接阿里云本文不做描述。
- 1、利用Python中的random模块中的choice方法random.choice()可以从任何序列,比如list列表中,选取一个随机的
- 最近接了个项目,其中有需求是要实现摇一摇红包功能,在网上搜了好久,都没有找到源码,没办法,只有自动写了,下面小编把我的劳动成果分享给大家供大
- 一、字符串(str)字符串转换为列表使用list()方法str_1 = "1235"str_2 = 'zhang
- 这个帖子在51js看到的,觉得很有学习意义,看看高手们是怎么做的吧!提问者:infinte急求:正则或算法,JS VBS均可。要求:[1]支
- 1.用一个栈【python中可以用List】就可以解决,时间和空间复杂度都是O(n)# -*- coding: utf8 -*-# 符号表S
- 用Python随机生成学生姓名,三科成绩和班级数据,再插入到PostgreSQL中。模块用psycopg2 randomimport ran
- 并行查询其优势就是可以通过多个线程来处理查询作业,从而提高查询的效率。SQL Server数据库为具有多个CPU的数据库服务器提供并行查询的
- K-means算法介绍简单来说,K-means算法是一种无监督算法,不需要事先对数据集打上标签,即ground-truth,也可以对数据集进
- 在域环境下我没配置成果,也许是域用户的原因,因为我在生产环境下搞的,更改域用户需要重启SQLServer ,所以这个方法放弃了,只能用证书形
- Cookies的处理作用保存客户端的相关状态在爬虫中如果遇到了cookie的反爬如何处理?手动处理 在抓