python | argparse传递参数

通过命令行的形式来传递参数,使得配置参数容易起来。这里介绍python包argparse第一步:定义函数import osimport pandas as pdimport argparsedef get_onefile_result(filename, threshold): dftmp = pd.read_csv(filename) df = dftmp.ilo...
摘要由CSDN通过智能技术生成

通过命令行的形式来传递参数,使得配置参数容易起来。这里介绍python包argparse

核心函数解析

ArgumentParser.add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])
  • name or flags
    Either a name or a list of option strings, e.g. foo or -f, --foo. 用于标识参数的参数,必须有,通常以-或–来表示
from argparse import ArgumentParser
parse = ArgumentParser.add_argument('--s','-s')
## 保留1个或者2个均可。
  • action
    action - The basic type of action to be taken when this argument is encountered at the command line. 默认是store,表示存参数的值,store_const 表示以常量的形式来存储,append 列表,append_const 列表常量。

  • nargs
    nargs - The number of command-line arguments that should be consumed
    nargs=2 表示包含两个值的参数列表
    nargs=’*’ 表示任意个参数
    nargs=’+’ 表示至少一个参数

  • default
    默认值

  • type
    参数的数值类型

  • choices
    给定候选的值

  • required
    是否为必须给定的参数

  • help
    参数解释

  • dest
    这个参数相当于把位置或者选项关联到一个特定的名字

  • metavar
    这个参数用于help信息输出中

案例

  • 第一步:定义函数
import os
import pandas as pd
import
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值