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


第一次根据需求写脚本

第一个版本,用于通用的数据转换

原数据为需要构造目标格式里面的几个字段,用某分隔符分开

目标数据为用指定分隔符分割的字段,源文件字段填充其间,其他字段为0

主要涉及命令行参数的处理和文件操作

 
  1. #!/usr/bin/python   
  2. # -*- coding: utf-8 -*-   
  3. #dataformat.py   
  4. #this script change data from your source to the dest data format   
  5.   
  6. import os,getopt,sys  
  7.   
  8. #read file ,return lines of the file   
  9. def read_file(path):  
  10.   f = open(path,"r")  
  11.   lines = f.readlines()  
  12.   f.close()  
  13.   return lines  
  14.   
  15. #process one line   
  16. #now according to the order of to and the source file line order   
  17. #change to a more flexable way   
  18. def one_line_proc(parts,total,to,outsp):  
  19.     toindex = 0  
  20.     outline=""  
  21.     for i in range(1,total+1):  
  22.       if toindex!=len(to) and i==to[toindex]:  
  23.         outline+=parts[toindex]  
  24.         toindex+=1  
  25.       else:  
  26.         outline+="0"  
  27.       if i!=total:  
  28.         outline+=outsp  
  29.     return outline  
  30.   
  31. #hand from inpath to the outpath   
  32. def process(inpath,total,to,outpath,insp="\t",outsp="\t"):  
  33.   lines = read_file(inpath)  
  34.   f = open(outpath,"w")  
  35.   result=[]  
  36.   for line in lines:  
  37.      parts = line.strip("\n").split(insp)  
  38.      if len(parts) == len(to):  
  39.        outline = one_line_proc(parts,total,to,outsp)  
  40.        result.append(outline+"\n")  
  41.   f.writelines(result)  
  42.   f.close()  
  43.   
  44.   
  45. def main():  
  46.     try:  
  47.         opts,args = getopt.getopt(sys.argv[1:],"F:P:t:a:i:o:")  
  48.         if len(opts) < 3:  
  49.           print("the mount of params must great equal than 3")  
  50.           sys.exit(1)  
  51.         for op,value in opts:  
  52.           if op == "-i":  
  53.             inpath = value  
  54.           elif op == "-o":  
  55.             outpath = value  
  56.           elif op == "-t":  
  57.             total = int(value)  
  58.           elif op == "-a":  
  59.             to = value.split(",")  
  60.           elif op == "-F":  
  61.             insp = value.decode("string_escape")  
  62.           elif op == "-P":  
  63.             outsp = value.decode("string_escape")  
  64.         #print(opts)   
  65.         #print(args)   
  66.     except getopt.GetoptError:  
  67.         print("params are not defined well!")  
  68.       
  69.       
  70.     if 'outpath' not in dir():  
  71.       outpath = inpath+".dist"  
  72.       
  73.     if 'inpath' not in dir():  
  74.       print("-i param is needed,input file path must define!")  
  75.       sys.exit(1)  
  76.       
  77.     if 'total' not in dir():   
  78.       print("-t param is needed,the fields of result file must define!")  
  79.       sys.exit(1)  
  80.   
  81.     if 'to' not in dir():  
  82.       print("-a param is needed,must assign the field to put !")  
  83.       sys.exit(1)  
  84.   
  85.     if not os.path.exists(inpath):  
  86.       print("file : %s is not exists"%inpath)  
  87.       sys.exit(1)  
  88.   
  89.     tmp=[]  
  90.     for st in to:  
  91.       tmp.append(int(st))  
  92.     to=tmp  
  93.   
  94.   
  95.     if 'insp' in dir() and 'outsp' in dir():  
  96.       #print("path a")   
  97.       process(inpath,total,to,outpath,insp,outsp)  
  98.     elif 'insp' in dir():  
  99.       #print("path b")   
  100.       process(inpath,total,to,outpath,insp)  
  101.     elif 'outsp' in dir():  
  102.       #print("path c")   
  103.       process(inpath,total,to,outpath,outsp=outsp)  
  104.     else:  
  105.       #print("path d")   
  106.       process(inpath,total,to,outpath)  
  107.   
  108. #if __name__ =="__main__":   
  109. main()  

相关内容