Day6 ConfigParser module yaml module, configparseryaml

Source: Internet
Author: User

Day6 ConfigParser module yaml module, configparseryaml

Yaml module:

Python can process the yaml file. The yaml File Installation Method is: $ pip3 install pyyaml


The configparser module is used to process files. It can add, delete, modify, and Query files.

Configparser is used to process files in a specific format. In essence, it uses open to operate files.

Let's take a look at the functions of the configarser module:

[DEFAULT]serveraliveinterval = 45compression = yescompressionlevel = 9forwardx11 = yes[bitbucket.org]user = hg[topsecret.server.com]host port = 50022forwardx11 = no

The above code format is a common configparser format, which is common in some places.

Import configparserconfig = configparser. configParser () config ["DEFAULT"] = {"ServerAliveInterval": "45", "compression": "yes", "CompressionLevel ": "9"} config ["bitbucket.org"] = {} config ["bitbucket.org"] ["user"] = "hg" config ["topsecret.server.com"] ={}# defines empty dictionary topsecret = config ["topsecret.server.com"] # assign an empty dictionary to topsecret, generate an empty dictionary topsecret ["Host Port"] = "50022" # Add a key-value pair to the dictionary topsecret ["Forwardx11"] = "no" config ["DEFAULT"] [" forwardx11 "] =" yes "with open (" config_file.ini ", "w") as configfile: config. write (configfile)

Run the following command:
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes

[bitbucket.org]
user = hg

[topsecret.server.com]
host port = 50022
forwardx11 = no

From the code above, we can see that the file format is similar to the dictionary format, including the keys and values types. We must first define a configparser file and then add a key-value pair to the file. As shown in the code above:

After writing the dictionary into a file, how can we read it? Next let's take a look at the file operations of configparser. ConfigParser:

Read:

>>> Import configparser
>>> Config = configparser. ConfigParser () # defines the information of a file.
>>> Config. sections ()
[]

  >>> config.read( 'example.ini' )   [ 'example.ini' ] >>> Config. sections ()['Bitbucket. org ', 'topsecret .server.com'] let's take a look at the addition, deletion, modification, and query of files in the configparser module:
Import configparserconfig = configparser. configParser () config. read ("config_file.ini") # delete the file field sec = config. remove_section ("bitbucket.org") config. write (open ("config_file.ini", "w") # Add a new field to the file # sec = config. has_section ("alex") # config. add_section ("alex") # config ["alex"] ["age"] = "21" # config. write (open ("config_file.ini", "w") # modify the file information in the field config. set ("alex", "age", "28") config. write (open ("config_file.ini", "w "))

In the field of the code above, we have implemented the add, delete, modify, and query function. You only need to know the functions in the file. In addition, operations can be performed on files in the same way. They are implemented in different ways and the code is written differently.

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.