https://docs.python.org/3/library/configparser.html

configparser Used to process files in a specific format , It is essentially the use of open To manipulate the file .

# notes 1
; notes 2 [section1] # node
k1 = v1 # value
k2:v2 # value [section2] # node
k1 = v1 # value

Set the format

1、 Get all nodes

import configparser
config = configparser.ConfigParser()
config.read('xxxooo', encoding='utf-8')
ret = config.sections()
print(ret)

2、 Get all the key value pairs under the specified node

import configparser
config = configparser.ConfigParser()
config.read('xxxooo', encoding='utf-8')
ret = config.items('section1')
print(ret)

3、 Gets all the nodes under the specified node

import configparser
config = configparser.ConfigParser()
config.read('xxxooo', encoding='utf-8')
ret = config.options('section1')
print(ret)

4、 Get the specified... Under the specified node key Value

import configparser
config = configparser.ConfigParser()
config.read('xxxooo', encoding='utf-8') v = config.get('section1', 'k1')
# v = config.getint('section1', 'k1')
# v = config.getfloat('section1', 'k1')
# v = config.getboolean('section1', 'k1') print(v)

5、 Check 、 Delete 、 Add a node

import configparser
config = configparser.ConfigParser()
config.read('xxxooo', encoding='utf-8') # Check
has_sec = config.has_section('section1')
print(has_sec) # Add a node
config.add_section("SEC_1")
config.write(open('xxxooo', 'w')) # Delete node
config.remove_section("SEC_1")
config.write(open('xxxooo', 'w'))

6、 Check 、 Delete 、 Set the key value pairs in the specified group

import configparser
config = configparser.ConfigParser()
config.read('xxxooo', encoding='utf-8') # Check
has_opt = config.has_option('section1', 'k1')
print(has_opt) # Delete
config.remove_option('section1', 'k1')
config.write(open('xxxooo', 'w')) # Set up
config.set('section1', 'k10', "")
config.write(open('xxxooo', 'w'))

python Module configparser More articles about

  1. Python Module : ConfigParser Profile read

    Python Module : ConfigParser Profile read   ConfigParser For reading and writing similar to INI Profile of file , The contents of the configuration file can be organized into groups , Multiple option values are also supported (option-value) type . ...

  2. python Module -configparser

    python modular configparser    Profile module import configparser    config = configparser.ConfigParser() config[&q ...

  3. Python Module learning - ConfigParser

    The configuration file A lot of software uses configuration files , image git When it runs, it reads ~/gitconfig,MySQL When it runs, it reads /etc/my.cnf,Python Package management tools provided pip command , I'll read it as well ~/.pip/ ...

  4. Python Module ConfigParser - Read and write configuration files

    Python Standard library ConfigParser The module provides a set of API To read and manipulate configuration files . The format of the configuration file a) The configuration file contains one or more section, Every section Have their own opt ...

  5. Python Module continued configparser、shutil、XML、paramiko、 System commands 、

    One .configparse # notes 1 ; notes 2 [section1] # node k1 = v1 # value k2:v2 # value [section2] # node k1 = v1 # value 1. Get all nodes ...

  6. Python modular :configparser、hashlib、(subprocess)

    configparser modular : This module is used to generate and modify common configuration documents . A common configuration file (.ini The suffix of ) The format is as follows : [DEFAULT] # DEFAULT It refers to the default content in the following dictionaries Serve ...

  7. python modular :configparser

    """Configuration file parser. A configuration file consists of sections, lead by a &q ...

  8. python Module ConfigParser: use python Parse configuration file

    It is very common to use configuration file to configure some parameters flexibly in the program , Parsing configuration files is not complicated , stay python It's even more so in my life , There are libraries that do this in the official library , That's it ConfigParser, Here is a brief introduction ...

  9. Python Module ConfigParser: use Python Parse configuration file

    It is very common to use configuration file to configure some parameters flexibly in the program , Parsing configuration files is not complicated , stay Python It's even more so in my life , There are libraries that do this in the official library , That's it ConfigParser, It's easy to do ...

Random recommendation

  1. 【 turn 】 Encapsulate native JS Realization Ajax

    function createXHR() { if (window.XMLHttpRequest) { //IE7+.Firefox.Opera.Chrome and Safari return new X ...

  2. my iOS The way 2

    No one has read the first one ... ok ... Then take notes ... The first one is about creating a helloworld, Maybe I bumped into someone else , So it's a little boring , Change your name good , Start writing the second I didn't do the first application before ...

  3. redis pub/sub Publish subscribe

    Redis The list data structure of is blpop and brpop command , Can return from the list and delete the first ( Or the last one ) Elements , Or blocked up , Until there's an element to work on . This can be used to implement a simple queue .( Reference resources :http://www.cn ...

  4. Https principle

    HTTPS There are actually two parts :HTTP + SSL / TLS, That is to say HTTP Add a layer of module to process encrypted information . Both the server and the client will transmit information through TLS To encrypt , So the transmitted data is encrypted data 1. ...

  5. SVG( Scalable vector graph ) Series of tutorials

    This series of tutorials is synced to the blog www.waylau.com Sort from new to old , Continuous updating SVG And JS Sketchpad for interactive instances SVG <marker> Create arrows SVG The map of China as an example SVG Examples of power switches SVG ...

  6. UVA - 10714 Ants

    The most time is that each ant chooses the longest way to crawl The least time is for each ant to choose the fastest way to crawl #include<iostream> #include<map> #include<s ...

  7. Remember cool dog music once API Acquisition , Interested can package and develop their own music player

    1. This tutorial is for personal study only , It is forbidden to be used for any commercial or illegal purpose , Please contact the author to delete the copyright issue . 2. The essay is the author's original document , Please indicate the source of the document :http://www.cnblogs.com/apresunday/ ...

  8. [BZOJ1207] [HNOI2004] Whac-A-Mole (dp)

    Description Mole is a kind of animal that likes digging holes very much , But after a certain period of time , It still likes to stick its head out to the ground for air . According to this characteristic Q Wrote a game to fight mole : In a n*n In the grid , At some point, the mole will be in a grid ...

  9. “Axure” Introduce

    One . Axure RP brief introduction : Axure RP It can help the designer of website demand , Quick and easy to create the sketch map of annotated page based on the website architecture diagram . Operation flow chart . And interaction design , And can automatically generate web files and specification files for demonstration , To provide demonstrations and ...

  10. d3js Visualization strategy

    d3js It's the idea of data driven graphics . It's basically understandable , What kind of graphics are there , Basically, there are data with similar structure . The steps are as follows : One . Adapting data formats This step is mainly for the second part , The result of the first step serves as a reference to the second part . such as , Painting level ...