欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python編程argparse入門(mén)淺析

 更新時(shí)間:2018年02月07日 16:51:26   作者:foryouslgme  
這篇文章主要介紹了Python編程argparse入門(mén)淺析,分享了相關(guān)代碼,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下

本文研究的主要是Python編程argparse的相關(guān)內(nèi)容,具體介紹如下。

#aaa.py
#version 3.5
import os    #這句是沒(méi)用了,不知道為什么markdown在編輯代碼時(shí),不加這一句,就不能顯示代碼高亮[汗]
import argparse


parser = argparse.ArgumentParser(description='Process some integers...')  #初始化一個(gè)分析器
#parser.add_argument(中的參數(shù))
#__init__(self, option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None)
parser.add_argument('integers',metavar='N',type=int,nargs='+',
          help='an integer for the accumulator')    
          #這是一個(gè)添加【位置參數(shù)】
          #第一個(gè)參數(shù)是自定義的參數(shù)名,在代碼中用來(lái)計(jì)算的(parser.parse_args().integers*2)


parser.add_argument('--sum',dest='accumulate',action='store_const',
          const=sum,default=max,
          help='sum the integers(default:find the max)')
          #這是一個(gè)添加【可選參數(shù)】
          #第一個(gè)參數(shù)是自定義的參數(shù)【在代碼中的使用parser.parse_args().sum】【在系統(tǒng)命令行中的使用:>python aaa.py --sum



args = parser.parse_args()
print(args)       #Namespace(accumulate=<built-in function sum>, integers2=[1, 2, 3, 4])
print(args.integers)  #integers要與上面的對(duì)應(yīng)
print(args.accumulate(args.integers))  #accumulate要與上面的對(duì)應(yīng)

在系統(tǒng)命令行中進(jìn)行參數(shù)調(diào)用結(jié)果如下:

D:\Program Files (x86)\Python35>python aaa.py -h
usage: aaa.py [-h] [--sum] N [N ...]

Process some integers...

positional arguments:
N an integer for the accumulator

optional arguments:
-h, --help show this help message and exit
--sum sum the integers(default:find the max)


D:\Program Files (x86)\Python35>python aaa.py 1 2 3 4 --sum
Namespace(accumulate=<built-in function sum>, integers2=[1, 2, 3, 4])
[1, 2, 3, 4]
10

D:\Program Files (x86)\Python35>python aaa.py 1 2 3 4
Namespace(accumulate=<built-in function max>, integers2=[1,2,3,4])
[1, 2, 3, 4]
4

在python交互模式下運(yùn)行結(jié)果如下:

附件

Keyword Arguments:
|
| - option_strings -- A list of command-line option strings which
| should be associated with this action.
|
| - dest -- The name of the attribute to hold the created object(s)
|
| - nargs -- The number of command-line arguments that should be
| consumed. By default, one argument will be consumed and a single
| value will be produced. Other values include:
| - N (an integer) consumes N arguments (and produces a list)
| - '?' consumes zero or one arguments
| - '*' consumes zero or more arguments (and produces a list)
| - '+' consumes one or more arguments (and produces a list)
| Note that the difference between the default and nargs=1 is that
| with the default, a single value will be produced, while with
| nargs=1, a list containing a single value will be produced.
|
| - const -- The value to be produced if the option is specified and the
| option uses an action that takes no values.
|
| - default -- The value to be produced if the option is not specified.
|
| - type -- A callable that accepts a single string argument, and
| returns the converted value. The standard Python types str, int,
| float, and complex are useful examples of such callables. If None,
| str is used.
|
| - choices -- A container of values that should be allowed. If not None,
| after a command-line argument has been converted to the appropriate
| type, an exception will be raised if it is not a member of this
| collection.
|
| - required -- True if the action must always be specified at the
| command line. This is only meaningful for optional command-line
| arguments.
|
| - help -- The help string describing the argument.
|
| - metavar -- The name to be used for the option's argument with the
| help string. If None, the 'dest' value will be used as the name.

總結(jié)

以上就是本文關(guān)于Python編程argparse入門(mén)淺析的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專(zhuān)題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

相關(guān)文章

  • 詳解解Django 多對(duì)多表關(guān)系的三種創(chuàng)建方式

    詳解解Django 多對(duì)多表關(guān)系的三種創(chuàng)建方式

    本文主要介紹了詳解解Django 多對(duì)多表關(guān)系的三種創(chuàng)建方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08
  • python操作yaml說(shuō)明

    python操作yaml說(shuō)明

    這篇文章主要介紹了python操作yaml說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04
  • 解決python和pycharm安裝gmpy2 出現(xiàn)ERROR的問(wèn)題

    解決python和pycharm安裝gmpy2 出現(xiàn)ERROR的問(wèn)題

    這篇文章主要介紹了python和pycharm安裝gmpy2 出現(xiàn)ERROR的解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • 解決python3爬蟲(chóng)無(wú)法顯示中文的問(wèn)題

    解決python3爬蟲(chóng)無(wú)法顯示中文的問(wèn)題

    下面小編就為大家分享一篇解決python3爬蟲(chóng)無(wú)法顯示中文的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • Python算法的時(shí)間復(fù)雜度和空間復(fù)雜度(實(shí)例解析)

    Python算法的時(shí)間復(fù)雜度和空間復(fù)雜度(實(shí)例解析)

    算法復(fù)雜度分為時(shí)間復(fù)雜度和空間復(fù)雜度,簡(jiǎn)單而講時(shí)間復(fù)雜度指的是語(yǔ)句執(zhí)行次數(shù),空間復(fù)雜度指的是算法所占的存儲(chǔ)空間,本文通過(guò)代碼給大家介紹Python算法的時(shí)間復(fù)雜度和空間復(fù)雜度問(wèn)題,感興趣的朋友一起看看吧
    2019-11-11
  • 使用python搭建服務(wù)器并實(shí)現(xiàn)Android端與之通信的方法

    使用python搭建服務(wù)器并實(shí)現(xiàn)Android端與之通信的方法

    今天小編就為大家分享一篇使用python搭建服務(wù)器并實(shí)現(xiàn)Android端與之通信的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-06-06
  • python去除刪除數(shù)據(jù)中\(zhòng)u0000\u0001等unicode字符串的代碼

    python去除刪除數(shù)據(jù)中\(zhòng)u0000\u0001等unicode字符串的代碼

    這篇文章主要介紹了python去除刪除數(shù)據(jù)中\(zhòng)u0000\u0001等unicode字符串的代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • python尋找含有關(guān)鍵字文件和刪除文件夾方式

    python尋找含有關(guān)鍵字文件和刪除文件夾方式

    這篇文章主要介紹了python尋找含有關(guān)鍵字文件和刪除文件夾方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • python內(nèi)置模塊之上下文管理contextlib

    python內(nèi)置模塊之上下文管理contextlib

    這篇文章介紹了python內(nèi)置模塊之上下文管理contextlib,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • 基于matplotlib xticks用法詳解

    基于matplotlib xticks用法詳解

    這篇文章主要介紹了基于matplotlib xticks用法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04

最新評(píng)論