Configparser module of the Python standard library series
This module provides the Configparser class which implements a basic configuration language which provides a structure sim Ilar to "s found in Microsoft Windows INI files. You can use the this to write Python programs which can is customized by end users easily.
Configparser is used to process files in a particular format, essentially using open to manipulate files.
The configuration file format is as follows:
# The first way of commenting; The second way of commenting [Node1] # node K1 = v1 # key = valuek2:v2 # Key:value
Instance
Create a file.conf file, content is empty, then enter python IDE:
[email protected] ~]# Touch file.conf [[email protected] ~]# Pythonpython 2.6.6 (r266:84292, Jul, 15:22:56) [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on Linux2type ' help ', ' copyright ', ' credits ' or ' license ' for more information.>> >
To add a node to a file
>>> Import configparser>>> config = Configparser. Configparser () >>> config.read (' file.conf ', encoding= ' utf-8 ') [' file.conf ']# add node ' node1 ', ' node2 ', then write to file >>> config.add_section ("Node1") >>> config.add_section ("Node2") >>> config.write (Open (' File.conf ', ' W '))
Check if the node exists
# returns "True" if the file exists, otherwise returns "False" >>> print (config.has_section (' Node1 ')) true>>> print (Config.has_ Section (' Node2 ')) true>>> print (config.has_section (' Node3 ')) False
Delete a node
# returns "True" if the deleted node is present, otherwise "False" >>> config.remove_section ("Node2") true>>> config.write (Open (' File.conf ', ' W ')) >>> print (config.has_section (' Node2 ')) False
Set a key-value pair within a node
# After adding key-value pairs don't forget to write to the file >>> config.set (' Node1 ', ' Name ', ' Ansheng ') >>> config.set (' Node1 ', ' Blog_url ', " Https://blog.ansheng.me ") >>> config.set (' Node1 ', ' Hostname '," Localhost.localhost ") >>> Config.set (' Node1 ', ' IP ', ' 127.0.0.1 ') >>> config.write (open (' file.conf ', ' W '))
Check if key exists in the node
# returns "True" if key of the node exists, otherwise "False" >>> print (config.has_option (' Node1 ', ' Name ')) true>>> print ( Config.has_option (' Node1 ', ' IP ')) true>>> print (config.has_option (' Node1 ', ' VV ')) False
Delete key within a node
# returns "True" if the deleted node exists, otherwise it returns "False" >>> config.remove_option (' Node1 ', ' IP ') true>>> config.write ( Open (' file.conf ', ' W ')) >>> print (config.has_option (' Node1 ', ' IP ')) False
Gets the value of the specified key under the specified node
# The default return is String type >>> config.get (' Node1 ', ' Name ') ' Ansheng ' >>> config.get (' Node1 ', ' blog_url ') ' https:// Blog.ansheng.me ' # Returns the string we can set into three data types, namely "int", "float", "bool" # v = config.getint (' node1 ', ' K1 ') # v = config.getfloat (' Node1 ', ' K1 ') # v = Config.getboolean (' node1 ', ' K1 ')
Gets all keys under the specified node
# Return all key lists below the node >>> config.options (' Node1 ') [' name ', ' Blog_url ', ' hostname ']
Gets all the key-value pairs under the specified node
# returns a list in which each tuple is a key-value pair >>> config.items (' Node1 ') [(' Name ', ' Ansheng '), (' Blog_url ', ' https:// Blog.ansheng.me '), (' hostname ', ' localhost.localhost ')]
Get all nodes
# get the number of nodes in the current file >>> config.sections () [' Node1 ']
#Python标准库 #Configparser
Configparser module of 11Python standard Library series