版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、需求驱动学习之Python(如何编写Python脚本替换文件中的多行字符?)2010-12-08 23:40 by 吴秦, 3636 visits, 收藏, 编辑 当一个人太执着于某一个东西的时候,会错过很多美好的东西!Python值得学习的一个工具,不要局限在当前使用的语言中。在大概3个月之前,Python对我来说一直是个迷。然而,就在3个月前我经理给我一个任务删除(替换)所有项目源码文件中包含特定几行内容的所有注释。整个项目源码的大小有1G,在Linux服务器(中高档)上编译需要半个多小时,可见代码量之大,不可能手动去一个一个改。肯定得用脚本去处理,于是我想到了Python。在这之前没有接
2、触过Python,花了2个星期一顿恶补之后,总算顺利交差了。一直很想和大家分享一下碰到的问题及我如何解决的(可能我的方案并不好,但是他能够解决我的问题),但一直拖到现在是因为我感觉我还对Python的了解还不够。因为要在短时间内完成上面交下来的任务,在学习Python的时候,都是走马观花,对解决自己的问题不相关的直接跳过,看资料也静不下心,脑海里都是问题。前几天我静下心把Python的书从头到尾浏览了一遍,感觉现在是时候要进行总结了。本文的主要内容如下:问题描述解题思路代码实现Python的特点1、问题描述项目源码很大,属于C/C+混合的那种,编程风格也很多样,有.c、.cc、cpp、.h、.
3、hh等文件。我要完成的任务是:把包含特定几行内容的注释删掉,如(声明:下面的内容只是我随便举的一个例子,项目源码中不涉及下面的内容。)/* * Copyright 2002 Sun Microsystems, Inc. All rights reserved.* Redistribution and use in source and binary forms, with or without* modification, are permitted provided that the following conditions* are met:* - Redistributions of s
4、ource code must retain the above copyright* notice, this list of conditions and the following disclaimer.* - Redistribution in binary form must reproduce the above copyright* notice, this list of conditions and the following disclaimer in* the documentation and/or other materials provided with the*
5、distribution.* Neither the name of Sun Microsystems, Inc. or the names of* contributors may be used to endorse or promote products derived* from this software without specific prior written permission.*/但是格式有很多种,如有的在“ Copyright 2002 Sun Microsystems, Inc. All rights reserved.”前面有一段关于本源码文件的描述、有的在“fro
6、m this software without specific prior written permission.”后面有一段关于本源码文件的描述、有的是C+风格的注释用/,而不是“/*/”、还有的没有“ * - Redistribution in binary form must reproduce the above copyright* notice, this list of conditions and the following disclaimer in* the documentation and/or other materials provided with the* d
7、istribution.”等等还有其他一些。总之一句话,我要删除的包含特定几行内容的注释有很多中格式!于是我决定要用Python来编写脚本处理。要匹配特定的内容,我想到了用正则表达式,但苦于不知道如何去构建正则来匹配上面描述的内容(您知道的话,希望能够告诉我)!我只有另辟路径了。2、解题思路我的思路要删除所有项目源码中包含特定几行内容的注释,脚本要满足以下几点功能:脚本要能够遍历所有的源码文件(.c、.cc、cpp、.h、.hh),并只处理上面的几种类型的文件找出包含特定几行内容的注释,并删除之能够处理一些特殊情况,如软连接文件上面的几点的处理步骤可以表示如下:Step 1:输入要处理源码文件
8、夹名,或者源码文件名;Step 2:如果是文件名,检查文件的类型是否为.c、.cc、cpp、.h、.hh,否则不处理;Step 3:检查文件是否是软连接,如果是软连接则不处理;Step 4:查找文件中是否存在匹配的注释,存在则删掉,否则不处理;Step 5:如果是文件夹,则对文件夹中的每个文件、文件夹进行处理,转Step2.思路很明确,关键是如何查找文件中是否包含匹配的内容,并删除!还有就是,对于一个没用过Python等脚本语言的人来说,如何编码实现也是一个问题!如何确定注释是否为包含特定几行内容的注释?我的思路如下:(因为正则表达式学的不好,只有通过下面的方法了)如果是/*、/则记录下当前的
9、文件行数,即行号startLine以行为单位查找是否存在特定的几行,如“ Copyright 2002 Sun Microsystems, Inc. All rights reserved.”等等直到遇到*/,或注释结束了(对于/)。如果存在,则记录下注释结束的行号endLine最后,删掉这从startLine endLine的内容。3、代码实现废话我不多说了,直接按照上面的实例实现代码,如果你对Python不熟,请参阅相关资料。#!/usr/bin/env python#Filename: comment.pyimport os, sys, fileinput#-def usage(): p
10、rint u help: comment.py dirname: Option, select a directory to operate filename: Option, select a file to operate Example: python comment.py /home/saylor/test #-def commentFile(src, fileList): description: comment files param src: Operate file name #if file exist? if not os.path.exists(src): print E
11、rror: file - %s doesnt exist.% src return False if os.path.islink(src): print Error: file - %s is just a link, will not handle it. return False filetype = (os.path.splitext(src)1 if not filetype in .c,.h: return False try: if not os.access(src, os.W_OK): os.chmod(src, 0664) except: print Error: you
12、can not chang %ss mode.% src try: inputf = open(src, r) outputfilename = src + .tmp outputf = open(outputfilename, w) beginLine = 0 endLine = isMatched = False #-find the beginLine and endLine - for eachline in fileinput.input(src): if eachline.find(/*) = 0: beginLine = fileinput.lineno() if eachlin
13、e.find(Copyright 2002 Sun Microsystems, Inc. All rights reserved.) = 0: isMatched = True if eachline.find(*/) = 0 and isMatched: endLine = fileinput.lineno() break #-delete the content between beginLine and endLine- print beginLine, endLine lineNo = 1 for eachline in inputf: if lineNo endLine: print
14、 eachline outputf.write(eachline) lineNo = lineNo + 1 inputf.close() outputf.close() os.rename(outputfilename, src) fileList.append(src) except: print Error: unexcept error. inputf.close() outputf.close() return True#-def commentDir(src, fileList): description: comment files in src(dir) param src: o
15、perate files in src(dir) #if dir exist? if not os.path.exists(src): print Error: dir - %s is not exist.%s (src) return False filelists = os.listdir(src) for eachfile in filelists: eachfile = src + / +eachfile if os.path.isdir(eachfile): commentDir(eachfile, fileList) elif os.path.isfile(eachfile): commentFile(eachfile, fileList) return True#-def main(): if len(sys.argv) 来创建函数对象。本质上,lambda需要一个参数,后面仅跟单个表达式作为函数体,而表达式的值被这个新建的函数返回。注意,即便是print语句也不能用在lambda形式中,只能使用表达式。24、exec、eval、assert、repr函数和反引号用来获取对象的可打印的表示形式。你可以通过定义类的_repr_方法来控制你的对象在被
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 幼儿园心理辅导工作制度
- 幼儿园教学年度工作制度
- 幼儿园新生查验工作制度
- 幼儿园汉语文字工作制度
- 幼儿园班里卫生工作制度
- 幼儿园科技教育工作制度
- 幼儿园落实督导工作制度
- 幼儿园采购小组工作制度
- 幼儿园食品生产工作制度
- 学校德育管理办法
- 容量评估与液体管理
- 宠物行业入股合同协议
- 抖音电商200个干货问题知识手册内部资料
- 刑法学知到智慧树章节测试课后答案2024年秋江西师范大学
- 2024年南昌二手房购买协议一
- 瓦斯隧道安全培训
- 2024年铁路机车司机乘务员知识(机考)试题库(含答案)
- 幼儿园 中班语言绘本《章鱼先生卖雨伞》
- 零星维修工程项目施工方案1
- 超星尔雅学习通《工程伦理》章节测试答案
- 人工智能训练师理论知识考核要素细目表五级
评论
0/150
提交评论