Congparser Module of Python Full Stack Road Standard Library Series

Keywords: Python Red Hat encoding Programming

configparser is used to process files in a specific format, which essentially uses open to manipulate files.

The configuration file format is as follows:

# The first way of annotation
; The second way of annotation

[node1]  # node
k1 = v1  # key = value
k2 : v2  # key : value

Example

Create a file.conf file with empty contents and enter Python IDE:

What I don't know in the process of learning can be added to me?
python learning communication deduction qun, 784758214
 There are good learning video tutorials, development tools and e-books in the group.
Share with you the current talent needs of python enterprises and how to learn python from zero foundation, and what to learn
[root@ansheng ~]# touch file.conf 
[root@ansheng ~]# python
Python 2.6.6 (r266:84292, Jul 23 2016, 15:22:56) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Adding nodes to files

>>> import configparser
>>> config = configparser.ConfigParser()
>>> config.read('file.conf', encoding='utf-8')
['file.conf']
# Add the nodes "node1","node2" and write to the file
>>> config.add_section("node1")
>>> config.add_section("node2")
>>> config.write(open('file.conf', 'w'))

Check whether the node exists

# Return "True" if the file exists, or "False"
>>> print(config.has_section('node1'))
True
>>> print(config.has_section('node2'))
True
>>> print(config.has_section('node3'))
False

Delete Nodes

# Return "True" if the deleted node exists, or "False"
>>> config.remove_section("node2")
True
>>> config.write(open('file.conf', 'w'))
>>> print(config.has_section('node2'))
False

Setting key-value pairs within nodes

# Don't forget to write to a file after adding key-value pairs
>>> 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 whether the key exists in the node

# Return "True" if the Key of the node exists, or "False"
>>> print(config.has_option('node1', 'Name'))
True
>>> print(config.has_option('node1', 'IP'))
True
>>> print(config.has_option('node1', 'VV'))
False

Delete the key in the node

# Return "True" if the deleted node exists, or "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 the string type
>>> config.get('node1', 'Name')
'ansheng'
>>> config.get('node1', 'Blog_URL')
'https://blog.ansheng.me'
# The returned string can be set to the following three data types: int, float and bool.
# v = config.getint('node1', 'k1')
# v = config.getfloat('node1', 'k1')
# v = config.getboolean('node1', 'k1')

Gets all key s under the specified node

# Returns all Key lists below the node
>>> config.options('node1')
['name', 'blog_url', 'hostname']

Gets all 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')]

If you are still confused in the world of programming, you can join our Python learning button qun: 784758214 to see how our predecessors learned! Exchange experience! I am a senior Python development engineer, from basic Python script to web development, crawler, django, data mining and so on, zero-based to the actual project data have been collated. To every Python buddy! Share some learning methods and small details that need attention. Click to join us. python learner gathering place

Get all nodes

# Gets how many nodes are in the current file
>>> config.sections()
['node1']

Posted by Giri J on Sun, 18 Aug 2019 23:29:38 -0700