"Python module" configparser module

Source: Internet
Author: User

Configparser module:

is the module used by the Python standard library to parse the configuration file.

Format:

Section: Using [] to mark section names

: or =: using: or = Assignment

[Websv]ip: ' 192.168.1.10 ' port:443name = ' root ' pw = ' root1990 ' definition: WEBSV called section

The same item can have multiple values:

IP: ' 192.168.1.11 ', ' 192.168.1.12 ', ' 192.168.1.13 ' #待测试

When the read configuration file is changed, the parameter name is automatically lowercase


There are multiple identical parameters under a section, only the last one can be read





Method, property name Parameters Role Example
Configparser ()
No
Creating an Configpaser instance
Read (filename)
File name in Filename:ini format
Open INI-formatted files
Sections ()
No
Returns all sections in list form
Items (section name)

Section name:

Specify section name

Returns the tuple of all the parameters and values of the specified section, in list form
Options (section name)

Section name:

Specify section name

Returns all the parameter names in a section in list form
Get[section Name][args Name]

Section name: Specify section name

Argsname: Specify Parameter name

Returns a single parameter value for a section.
Getint () \getboolean () \getfloat ()
Add_section (Section_name) Section_name: Specify section name Add a new section
Set (Section_name,args_name,value)

Section_name: Specify section name

Args_name: Specify Parameter name

Value: Sets the values of the parameters

Set the specific parameter values.
Remove_section (Section_name) Section_name: Specify section name Deletes the specified section
Remove_option (Section_name,args_name)

Section_name: Specify section name

Args_name: Specify Parameter name

Delete the args entry in the section
Clear () Empty all sections except Deafult
Write (open (file_name, ' W ')) Open (file_name, ' W ')): Opens a file in write mode Save the above edited information to file_name
Advanced Operation:
A single parameter value is multiple lines Except for the first line, add a space to the other line

args = "Line 1

Line 2 "

Results:

Line 1

Line 2

parameter values with variables url = http://% (host) s:% (port) s/Portal

[web]

Host = ' 192.168.0.1 '

Port = 8000

url = http://% (host) s:% (port) s/Portal

Results:

http://192.168.0.1 ': 8000/Portal

# # # #例一: Import configparserconfig = Configparser. Configparser () config.read (' Example.ini ') #section_name = [' webserver '] #args_name = [' IP ', ' Port ', ' url ']config.add_ Section (' webserver ') config.set (' webserver ', ' IP ', ' 192.168.0.1 ') config.set (' webserver ', ' Port ', 8000) config.set (' Webserver ', ' url ', ' http//:% (IP) s:% (port) s ') Config.write (open (' Example.ini ', ' W ')) print (Config.sections ()) Print ( Config.options ()) print (Config.items ()) print (config.get[' webserver ' [' url '])

"Python module" configparser module

Related Article

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.