mode one : Configuration file (configparser module )
mode two : Parse parameters (Argparse module)
1. Configuration file (Configparser module)
1.1 Configparser Introduction
Configparser is the package that is used to read the configuration file. The format of the configuration file is as follows: the brackets "[]" are included in the section. section below is a key-value-like options content. For example
[db]db_host = 127.0.0.1db_port = 22db_user = Rootdb_pass = rootroot [concurrent]thread = 10processor = 20
1.2 Configparser Initial work
Using Configparser preferred to initialize the instance and read the configuration file:
Import CONFIGPARSERCF = Configparser.configparser () cf.read ("Profile name")
1.3 Configparser function
1.3.1. Get all Sections
>>> s = cf.sections () >>> print s[' db ', ' concurrent ']
1.3.2 Obtaining options for a specified section
>>> cf.options (' db ') [' db_host ', ' db_port ', ' db_user ', ' Db_pass ']
1.3.3 Obtaining configuration information for the specified sections
>>> cf.items (' db ') [(' Db_host ', ' 127.0.0.1 '), (' Db_port ', ' ') ', (' Db_user ', ' Root '), (' Db_pass ', ' rootroot ')]
1.3.4 Get the option information for the specified sections
>>> cf.get ("db", "Db_host") ' 127.0.0.1 ' >>> cf.getint ("db", "Db_port") 22
There are also GetFloat, Getboolean
Note : All changes to the contents of the file should be written last.
1.3.5 setting the value of an option
>>> cf.set ("db", "Db_host", "127.1.1.1") >>> cf.write (Open ("Config.ini", ' W '))
The contents of the file after writing are
[db]db_host = 127.1.1.1db_port = 22db_user = Rootdb_pass = Rootroot[concurrent]thread = 10processor = 20
1.3.6 Add a section
>>> cf.add_section ("Jihite") >>> cf.set ("Jihite", "int", "+") >>> Cf.set ("Jihite", "bool", "True") >>> cf.set ("Jihite", "float", "3.14") >>> cf.write (Open ("Config.ini", ' W '))
The contents of the file after writing are
[db]db_host = 127.1.1.1db_port = 22db_user = Rootdb_pass = Rootroot[concurrent]thread = 10processor = 20[jihite]int = 15bo OL = Truefloat = 3.14
1.3.7 Remove a section
>>> cf.remove_option ("Jihite", "int") true>>> cf.write (Open ("Config.ini", ' W '))
The changed file is
[db]db_host = 127.1.1.1db_port = 22db_user = Rootdb_pass = Rootroot[concurrent]thread = 10processor = 20[jihite]bool = Tru Efloat = 3.14
1.3.8 Remove an option
>>> cf.remove_section ("Jihite") true>>> cf.write (Open ("Config.ini", ' W '))
The changed file is
[db]db_host = 127.1.1.1db_port = 22db_user = Rootdb_pass = Rootroot[concurrent]thread = 10processor = 20
1.4 Configparser Example
mode two : Parse parameters (Argparse module)
2. Parsing parameters (Argparse module)
2.1 Argparse Introduction
Argparse is a python command-line parsing tool, which is the recommended tool for writing command-line programs in the Python standard library.
2.2 Argparser Initial work
Import Argparseparser = Argparse. Argumentparser ()
The class Argumentparser is defined as:
Class Argumentparser (Prog=none, Usage=none, Description=none, Epilog=none, parents=[], Formatter_class=argparse. Helpformatter, prefix_chars= '-', Fromfile_prefix_chars=none, Argument_default=none, conflict_handler= ' error ', add_ Help=true)
The meaning of the parameter is
2.2.1 Prog: Name of the program, default is Sys.argv[0]
>>> parser = Argparse. Argumentparser (prog= "MyProgram") >>> parser.print_help () Usage:myprogram [-h]optional arguments:- H,-- Help Show this help message and exit
2.2.2 Usage: A string describing the purpose of a program
>>> parser = Argparse. Argumentparser (prog= "MyProgram", usage= "% (Prog) s [Options]") >>> parser.print_help () Usage:myprogram [ Options]optional arguments: -H,--help show this help message and exit
2.2.3 Description:help the text before the message
>>> parser.print_help () Usage:myprogram [options]create my own programoptional arguments: -H,--help Show this help message and exit
2.2.4 the text after Epilog:help
>>> parser = Argparse. Argumentparser (prog= "MyProgram", usage= "% (Prog) s [Options]", description= "Create My Own Program", epilog= "and that ' s How do you ' d foo a bar ') >>> parser.print_help () Usage:myprogram [options]create my own programoptional arguments:
-h,--help Show this help message and Exitand that's how you ' d foo a bar
2.2.5
More detailed reference
2.3 Adding parameter options
Http://www.cnblogs.com/linxiyue/p/3908623.html?utm_source=tuicool
Python parameter settings