[Python3] INI file read and write

Source: Internet
Author: User

Overview

INI is one of the configuration file formats that we often see.

INI is a file name extension (also commonly used in other systems) in the Microsoft Windows operating system.

INI "initialization (Initial)" is abbreviated. As the term indicates, the INI file is used to initialize or set parameters on the operating system or a specific program.

Its basic form is as follows:

= == = = = = = = Value4

We use the Python configparser module to read and write INI files.

Configparser Read
    • Read (filename) reads INI file contents

    • Sections () Gets all the sections and returns as a list

    • Options (sections) gets all option for the specified section

    • Get (section,option) Gets the value of option in section, returned as a string type

Write
    • Set (section, option, value) to update the option in sections

Instance

Our husband becomes an INI file and writes the data and then reads it out to show.

#-*-coding:utf-8-*-__author__='Gubei'#Import ModuleImportConfigparserif __name__=="__main__":    #build an object firstConfig =Configparser. Configparser ()#to get us to write a few sets of data    #first add a sectionConfig.add_section ("Open Source Optimization test")        #Add key-value Key-value pairs under the new sectionConfig.set ("Blog Park","Cnblog","Gubei") Config.set ("Cnblog","Gubei","Python3") Config.set ("Gubei","Pyhon3","INI file read")        #add another section, but no Key-value key value pairConfig.add_section ("the night has been so deep")        #Write FileWith open ('Iniconfig.ini','W') as Configfile:config.write (configfile)#####################################            #let's start by reading the INI file just now .Config.read ("Iniconfig.ini")        #get all of its sectionsSections =config.sections ()Print(sections)#get all the options under section     forSecinchsections:options=config.options (sec)Print(Options)#get the corresponding value value according to sections and options     forSecinchsections: forOptioninchconfig.options (sec):Print("[%s]%s=%s"% (sec, option, Config.get (sec, option)))
Summary

The INI file scenario is typically used to initialize the configuration file, but you can also use it for data storage if you wish.

To keep a small problem, try encapsulating a generic INI file operation class with your class.

[Python3] INI file read and write

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.