设为首页 收藏本站
查看: 832|回复: 0

[经验分享] Python脚本示例[命令行参数,函数,判定,退出等]

[复制链接]

尚未签到

发表于 2017-5-6 12:45:43 | 显示全部楼层 |阅读模式
  第一次根据需求写脚本
  第一个版本,用于通用的数据转换
  原数据为需要构造目标格式里面的几个字段,用某分隔符分开
  目标数据为用指定分隔符分割的字段,源文件字段填充其间,其他字段为0
  主要涉及命令行参数的处理和文件操作
#!/usr/bin/python# -*- coding: utf-8 -*-#dataformat.py#this script change data from your source to the dest data formatimport os,getopt,sys#read file ,return lines of the filedef read_file(path):f = open(path,"r")lines = f.readlines()f.close()return lines#process one line#now according to the order of to and the source file line order#change to a more flexable waydef one_line_proc(parts,total,to,outsp):toindex = 0outline=""for i in range(1,total+1):if toindex!=len(to) and i==to[toindex]:outline+=parts[toindex]toindex+=1else:outline+="0"if i!=total:outline+=outspreturn outline#hand from inpath to the outpathdef process(inpath,total,to,outpath,insp="\t",outsp="\t"):lines = read_file(inpath)f = open(outpath,"w")result=[]for line in lines:parts = line.strip("\n").split(insp)if len(parts) == len(to):outline = one_line_proc(parts,total,to,outsp)result.append(outline+"\n")f.writelines(result)f.close()def main():try:opts,args = getopt.getopt(sys.argv[1:],"F:P:t:a:i:o:")if len(opts) < 3:print("the mount of params must great equal than 3")sys.exit(1)for op,value in opts:if op == "-i":inpath = valueelif op == "-o":outpath = valueelif op == "-t":total = int(value)elif op == "-a":to = value.split(",")elif op == "-F":insp = value.decode("string_escape")elif op == "-P":outsp = value.decode("string_escape")#print(opts)#print(args)except getopt.GetoptError:print("params are not defined well!")if 'outpath' not in dir():outpath = inpath+".dist"if 'inpath' not in dir():print("-i param is needed,input file path must define!")sys.exit(1)if 'total' not in dir(): print("-t param is needed,the fields of result file must define!")sys.exit(1)if 'to' not in dir():print("-a param is needed,must assign the field to put !")sys.exit(1)if not os.path.exists(inpath):print("file : %s is not exists"%inpath)sys.exit(1)tmp=[]for st in to:tmp.append(int(st))to=tmpif 'insp' in dir() and 'outsp' in dir():#print("path a")process(inpath,total,to,outpath,insp,outsp)elif 'insp' in dir():#print("path b")process(inpath,total,to,outpath,insp)elif 'outsp' in dir():#print("path c")process(inpath,total,to,outpath,outsp=outsp)else:#print("path d")process(inpath,total,to,outpath)#if __name__ =="__main__":main()  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-373830-1-1.html 上篇帖子: Python实例讲解 -- 发送邮件带附件 (亲测) 下篇帖子: 【Python旧时笔记 五】PyDictObject头文件阅读
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表