1. Create a configuration file
Create a configuration file on the D drive with the name: test.ini
The content is as follows:
[ baseconf]
host=127.0.0.1
port=3306
user=root
password=root
db_name=gloryroad
[ test]
ip=127.0.0.1
int=1
float=1.5
bool=True
Note: To save the file as ansi encoding, utf-8 encoding will report an error
[Baseconf] in the file is section
Second, read the configuration file
import ConfigParser
cf=ConfigParser.ConfigParser()
cf.read(path) Read configuration files (ini, conf) and return the result is a list
cf.sections() Get all the sections (domains) read, return the list type
cf.options('sectionname') All keys under a certain domain, return list type
cf.items('sectionname') All key and value pairs under a certain domain
value=cf.get('sectionname','key') Get the value corresponding to the key under a certain yu
cf.type(value) The type of value obtained
(1)getint(section, option)
Get the value of option in the section and return int type data, so this function can only read the value of int type.
(2)getboolean(section, option)
Get the value of option in the section and return boolean data, so this function can only read the value of boolean type.
(3)getfloat(section, option)
Get the value of option in the section and return floating-point data, so this function can only read floating-point values.
(4)has_option(section, option)
Check whether the specified option exists in the specified section, and return True if it exists, otherwise return False.
(5)has_section(section)
Check whether the specified section exists in the configuration file, and return True if it exists, otherwise return False.
Three, dynamically write configuration files
cf.add_section('test') add a domain
cf.set('test3','key12','value12') add a key value pair under the domain
cf.write(open(path,'w')) To use'w'
learn to fail, failure to learn
Content expansion:
Python use configuration file process
Expose variables to users for modification through configuration files
Standard library module configparser, which can use standard format in configuration files.
The configuration file must be divided into sections with headings such as [files] and [colors]. The name of the title can be specified at will, but they must be enclosed in square brackets.
$ cat area.ini
[ numbers]
pi:3.1415926535893971[messages]
greeting: Welcome to the area calutation program!
question: plse enter the radius
result_message: The area is
Use python to read him
from configparser import ConfigParser
CONFIGFILE ="area.ini"
config =ConfigParser()
# Read configuration file
config.read(CONFIGFILE)print(config['messages'].get('greeting'))
radius =float(input(config['messages'].get('question')+' '))
# End with a space to continue printing on the current line:
print(config['messages'].get('result_message'),end=' ')print(config['numbers'].getfloat('pi')* radius**2)
This is the end of this article on how to write a configuration file for python. For more related methods of writing configuration files in python, please search for ZaLou.Cn's previous articles or continue to browse related articles below. I hope you will support ZaLou more in the future. Cn!
Recommended Posts