optparser模块主要用来为脚本传递命令参数功能。
用optparse模块可以在命令行输入相应的参数和参数值进行交互式操作.提高了代码的复用性.
// optparser模块的引入和初始化 from optparse import OptionParser optparser = OptionParser()3.命令行参数设置
// An highlighted block
optparser.add_option(
"-f",
"--inputFile",
action="store",
dest="input",
help="filename containing csv",
default=None
)
optparser.add_option(
"-s",
"--minSupport",
dest="minS",
help="minimum support value",
default=0.15,
type="float",
)
optparser.add_option(
"-c",
"--minConfidence",
dest="minC",
help="minimum confidence value",
default=0.6,
type="float",
)
optparser.add_option(
"-u",
"--url",
action="store_true",
dest="url",
help="server url",
type="str",
)
optparser.add_option(
"-n",
"--number",
action="store_true",
dest="number",
help="person number",
type="int",
)
4.获取命令行输入的参数
// 获取命令行输入的参数 (options, args) = optparser.parse_args() input = options.input minS = options.minS minC = options.minC url = options.url number = options.number5.optparser.add_option()相关参数解释
// optparser.add_option()相关参数解释 action:默认值为store,值为store时,在命令行中需要填入相关参数的值;置为store_true时,命令行中相关参数的值可以省略 //根据步骤3的命令行参数设置,我们的命令行可以写成: python xxx.py -f DATASET.csv -s 0.15 -c 0.16 -u -n dest:设置命令行中参数名称,也称为键 help:参数的描述 type:参数值类型 default:参数默认值6.命令行展示
//根据步骤3的命令行参数设置,我们的命令行可以写成: python xxx.py -f DATASET.csv -s 0.15 -c 0.16 -u -n



