#腳本作用:迴歸測試,用最新版本跑出的數據與上一個版本跑出的數據進行對比
#Python 版本:3.7.2,
#!/usr/bin/python # -*- coding: utf-8 -*- import os import sys import struct import time, datetime from optparse import OptionParser def CompareCRD(sou,des,key): source_date = open(sou,'r') destination_data = open(des,'r') HNAG_NUM=0 source_line = source_date.read().splitlines() isBool = 0 for source_word in source_line: source = source_word.split("|") destination_data.seek(0, 0) destination_line = destination_data.read().splitlines() HNAG_NUM +=1 WAI_NUM = 0 for destination_word in destination_line: destination = destination_word.split('|') isBool = 0 WAI_NUM +=1 for i in key: if source[i-1] == destination[i-1]: continue else: isBool = 1 if isBool == 0: print("Source row: {0} ,Destination {1}".format(HNAG_NUM, WAI_NUM)) min = len(source) if len(source) < len(destination) else len(destination) for j in range(min): if source[j] != destination[j]: print("Source not equal : {0} , Destination not equal :{1}".format(source[j],destination[j])) break if isBool == 0: print("This row is equal") print('') else: print("This row {} is not not equal".format(HNAG_NUM)) print('') if __name__ == '__main__': paster = OptionParser(description="整個文件功能一類的幫助信息") #構造對象 #action是有store,store_true,store_false等,dest是存儲的變量,default是缺省值,help是幫助提示 paster.add_option("-s", "--srource", dest="src",type='string') paster.add_option("-d", "--dstination", dest="dst",type='string') (options, ignore) = paster.parse_args() print("srcfile = %s dstfile = %s" %(options.src, options.dst)) key=[14,15,18] source = options.src destination = options.dst CompareCRD(source,destination,key)
遇到問題:因爲是在PyCharm 上編寫的,在移植到Linux上後,
運行報錯:linux TabError: inconsistent use of tabs and spaces in indentation。空格和tab縮寫造成的問題。
optionparser 模塊講解
parser = OptionParser(description="整個文件功能一類的幫助信息")
parser.add_option("-x", "--xxx", dest="xxx", action="store",
help="單個參數的幫助信息", metavar="希望接受值的示例") # metavar用於提示希望接受的值示例
parser.add_option("-n", "--num", dest="num", action="store", type="int", metavar="number",#type指明瞭類型,默認爲string
help="單個參數的幫助信息,有默認值,整型", default=1) # 如果參數中沒明確給出值,則設爲default的值
parser.add_option("-t", "--settrue", dest="testbool", action="store_true",
# 這個類型的添加metavar無效,用於判斷是否出現了某個參數,如果出現了爲ture
help="Test bool")
parser.add_option("-f", "--setfalse", dest="testbool", action="store_false",
# 這個類型的添加metavar無效,用於判斷是否出現了某個參數,如果出現了爲false
help="Test bool", default=False) # 如果都出現以最後的出現爲準,如果都沒出現以有default的爲準
(options, args) = parser.parse_args()
if options.xxx ==True:
print('who is xxx ')
add_option()參數說明:
action:存儲方式,分爲三種store、store_false、store_true
type
:類型
dest:存儲的變量
default:默認值
help
:幫助信息
--store_false fakeArgs 中存在'-v'verbose將會返回False,而不是‘how are you’,也就是說verbose的值與'-v'的後一位無關,只與‘-v’存在不存在有關。
--store_ture 這與action="store_false"類似,只有其中有參數'-v'存在,則verbose的值爲True,如果'-v'不存在,那麼verbose的值爲None