Python 调用optionparser模块,自动对比数据

#脚本作用:回归测试,用最新版本跑出的数据与上一个版本跑出的数据进行对比
#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 上表示命令行参数的值保存在options对象中。例如上面一段代码,如果我们对optParser.parse_args()函数传入的参数列表中带有‘-f’,那么就会将列表中‘-f’的下一个元素作为其dest的实参filename的值,他们两个参数形成一个字典中的一个元素{filename:file_txt}。相反当我们的参数列表中没有‘-f’这个元素时,那么filename的值就会为空。
--store_false fakeArgs 中存在'-v'verbose将会返回False,而不是‘how are you’,也就是说verbose的值与'-v'的后一位无关,只与‘-v’存在不存在有关。
--store_ture  这与action="store_false"类似,只有其中有参数'-v'存在,则verbose的值为True,如果'-v'不存在,那么verbose的值为None
最后通过parse_args()函数的解析,获得选项,parse_args() 返回的两个值:options,它是一个对象(optpars.Values),保存有命令行参数值。只要知道命令行参数名,就可以访问其对应的值 。args,它是一个由 positional arguments 组成的列表。

 

posted on 2021-01-13 11:02  如尘如水  阅读(112)  评论(0编辑  收藏  举报