歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Python腳本示例[命令行參數,函數,判定,退出等]

Python腳本示例[命令行參數,函數,判定,退出等]

日期:2017/3/1 10:48:58   编辑:Linux編程

第一次根據需求寫腳本

第一個版本,用於通用的數據轉換

原數據為需要構造目標格式裡面的幾個字段,用某分隔符分開

目標數據為用指定分隔符分割的字段,源文件字段填充其間,其他字段為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. import os,getopt,sys
  6. #read file ,return lines of the file
  7. def read_file(path):
  8. f = open(path,"r")
  9. lines = f.readlines()
  10. f.close()
  11. return lines
  12. #process one line
  13. #now according to the order of to and the source file line order
  14. #change to a more flexable way
  15. def one_line_proc(parts,total,to,outsp):
  16. toindex = 0
  17. outline=""
  18. for i in range(1,total+1):
  19. if toindex!=len(to) and i==to[toindex]:
  20. outline+=parts[toindex]
  21. toindex+=1
  22. else:
  23. outline+="0"
  24. if i!=total:
  25. outline+=outsp
  26. return outline
  27. #hand from inpath to the outpath
  28. def process(inpath,total,to,outpath,insp="\t",outsp="\t"):
  29. lines = read_file(inpath)
  30. f = open(outpath,"w")
  31. result=[]
  32. for line in lines:
  33. parts = line.strip("\n").split(insp)
  34. if len(parts) == len(to):
  35. outline = one_line_proc(parts,total,to,outsp)
  36. result.append(outline+"\n")
  37. f.writelines(result)
  38. f.close()
  39. def main():
  40. try:
  41. opts,args = getopt.getopt(sys.argv[1:],"F:P:t:a:i:o:")
  42. if len(opts) < 3:
  43. print("the mount of params must great equal than 3")
  44. sys.exit(1)
  45. for op,value in opts:
  46. if op == "-i":
  47. inpath = value
  48. elif op == "-o":
  49. outpath = value
  50. elif op == "-t":
  51. total = int(value)
  52. elif op == "-a":
  53. to = value.split(",")
  54. elif op == "-F":
  55. insp = value.decode("string_escape")
  56. elif op == "-P":
  57. outsp = value.decode("string_escape")
  58. #print(opts)
  59. #print(args)
  60. except getopt.GetoptError:
  61. print("params are not defined well!")
  62. if 'outpath' not in dir():
  63. outpath = inpath+".dist"
  64. if 'inpath' not in dir():
  65. print("-i param is needed,input file path must define!")
  66. sys.exit(1)
  67. if 'total' not in dir():
  68. print("-t param is needed,the fields of result file must define!")
  69. sys.exit(1)
  70. if 'to' not in dir():
  71. print("-a param is needed,must assign the field to put !")
  72. sys.exit(1)
  73. if not os.path.exists(inpath):
  74. print("file : %s is not exists"%inpath)
  75. sys.exit(1)
  76. tmp=[]
  77. for st in to:
  78. tmp.append(int(st))
  79. to=tmp
  80. if 'insp' in dir() and 'outsp' in dir():
  81. #print("path a")
  82. process(inpath,total,to,outpath,insp,outsp)
  83. elif 'insp' in dir():
  84. #print("path b")
  85. process(inpath,total,to,outpath,insp)
  86. elif 'outsp' in dir():
  87. #print("path c")
  88. process(inpath,total,to,outpath,outsp=outsp)
  89. else:
  90. #print("path d")
  91. process(inpath,total,to,outpath)
  92. #if __name__ =="__main__":
  93. main()

Copyright © Linux教程網 All Rights Reserved