"Python" Configuration parsing configparser & command-line argument parsing optparser

Source: Internet
Author: User

Configparser

Configparser wraps the Read and write of the configuration file, making it easier for the Python program to manipulate the configuration file. The configuration file here refers to the type of. ini file, the basic format is as follows

[section_a]  == a_value2[section_b]== = b _value3

A file is separated into sections, and there are many key-value pairs in each section to build the configuration file in such a way.

* Each value is read into the program by default is the STR variable, so do not add quotation marks in the configuration file

Program usage

ImportCONFIGPARSERCF=Configparser.configparser () cf.read ('Path')#read the file, but even if you can't find it, you won't get an error, just return an empty list####### #读配置 ########Cf.sections ()#returns a list of all sectionsCf.options ('Section name')#Read all the option in a section, such as return [' A_key1 ', ' A_key2 ')Cf.items ()#return [(' A_key1 ', ' a_value1 '),...] ThisCf.get ('section_a','A_key1')#read a value.####### #写配置 ########" "read a file, which is equivalent to loading the contents of this file into memory, and then you can do some of the following to modify it" "Cf.set ('section_a','A_key3','A_value3')#add a new record, where the key can be written to exist, that is, to update the existing value valuesCf.add_section ('Section_c') cf.write (open ("Path","W"))#Finally, the memory of the changed things into a file, it is worth noting that the parameter is not a path but a file Object! 

Optparser

Optparser and Configparser have nothing to do with = =. But at the time of learning to feel they look very similar, followed by the word before the habit, the look like all to come down to a together. And then just the two things are not much, just write it in one piece

Optparser is primarily supported for scripting with command-line parameters. General script only sys.argv command line parameters, and Optparser can have like NETSTAT-NTLP such as "bar" xx command, the function is much stronger.

First, or look at the main use:

Generally, the operation of the Optparser is written in the main function, after all, the external parameters to the first time to resolve, to determine exactly what work to do. For these operations, the first is to set command-line arguments:

Parser = Optparser. Optionparser (usage='usage:%prog [option]')#The usage parameter configuration string is used to illustrate the use of the command-line arguments provided by this parser. Prints the relevant string when the argument is-H or--help. It is recommended to add this usagePaser.add_option ('- T','--test', dest='test_variable', default='default', type='string', help='It is a test option', action='Store')" "This sentence is the most important optparser the statement that sets the command line argument. The meanings of several parameters of Add_option are: parameter short identification, parameter long identification, storing variable name, default value, storage type, help information, storing action. The values of all parameters are strings. It is worth mentioning that the default value of type is ' string ', action is generally set to ' store '" "

After setting up a few parameters, you can parse it, you need to make a statement:

(Options,args) = Parser.parse_args ()

All command-line arguments passed to the script will then be stored in the options. XXX in this variable, XXX is what is determined by the dest of each parameter set before. For example, before the-t parameter is set, the value after the command line-t is stored in the options.tset_variable as a string, which can be used by subsequent programs.

In addition, when setting parameters, it is not specified which parameters are necessary, which are optional, which can not be followed by the value only need one-X to appear on the line and so on. I have not studied these carefully, but I have done this before, after the parsing is done, use the parameter values to make a judgment on all the parameters obtained. See if there are parameters that are not passed in, or if the value is not legal, and so on, to do a check.

"Python" Configuration parsing configparser & command-line argument parsing optparser

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.