New pose of configuration file: configparser

Keywords: Python encoding

  • About ConfigParser

    ConfigParser is a package used to read configuration files. The format of the configuration file is as follows: the brackets "[]" contain sections. section below is the configuration similar to key value.

  • ConfigParser usage:

       [db]
       db_host = 127.0.0.1
       db_port = 69
       db_user = root
       db_pass = root
       host_port = 69
       
       [concurrent]
       thread = 10
       processor = 20
       
    1. ConfigParser initialization object

        import configparser
        config = configparser.ConfigParser()
        config.read("ini", encoding="utf-8")
        r = config.options("db")
        print(r)
        #Operation result
        # ['db_host', 'db_port', 'db_user', 'db_pass', 'host_port']
      
    2. Get the value of pointing option under pointing section

        import configparser
        config = configparser.ConfigParser()
        config.read("ini", encoding="utf-8")
        r = config.get("db", "db_host")
        # r1 = config.getint("db", "k1") #Convert get to value to int
        # r2 = config.getboolean("db", "k2" ) #Convert get to value to bool type
        # r3 = config.getfloat("db", "k3" ) #Convert get to value to floating point
        print(r)
        #Operation result
        # 127.0.0.1
      
      
    3. Get the configuration information of the pointing section

      import configparser
      config = configparser.ConfigParser()
      config.read("ini", encoding="utf-8")
      r = config.items("db")
      print(r)
      #Operation result
      #[('db_host', '127.0.0.1'), ('db_port', '69'), ('db_user', 'root'), ('db_pass', 'root'), ('host_port', '69')]
    4. Modify the value of an option. If it does not exist, it will create

      # Modify the value of an option. If the option does not exist, it will be created
      import configparser
      config = configparser.ConfigParser()
      config.read("ini", encoding="utf-8")
      config.set("db", "db_port", "69")  #Change the value of DB port to 69
      config.write(open("ini", "w"))
    5. Check whether section or option exists, bool value

        import configparser
        config = configparser.ConfigParser()
        config.has_section("section") #Whether the section exists
        config.has_option("section", "option")  #Whether the option exists
      
      
    6. Add section and option

        import configparser
        config = configparser.ConfigParser()
        config.read("ini", encoding="utf-8")
        if not config.has_section("default"):  # Check if section exists
            config.add_section("default")
        if not config.has_option("default", "db_host"):  # Check if the option exists
            config.set("default", "db_host", "1.1.1.1")
        config.write(open("ini", "w"))
        
       
    7. Delete section and option

      import configparser
      config = configparser.ConfigParser()
      config.read("ini", encoding="utf-8")
      config.remove_section("default") #All content under the whole section will be deleted
      config.write(open("ini", "w"))
    8. write file

      # The following lines of code only read the contents of the file into memory. After a series of operations, the file must be written back to take effect.
      import configparser
      config = configparser.ConfigParser()
      config.read("ini", encoding="utf-8")
      #Write back file
      config.write(open("ini", "w"))

Posted by finger on Fri, 18 Oct 2019 12:20:43 -0700