How to write python configuration file

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

How to write python configuration file
How to write classes in python
How to write return in python
How to write win programs in python
How to write try statement in python
How to switch the hosts file using python
Teach you how to write games in python
How to write a confession program in python
How to read and write files with Python
How to deal with python file reading failure
How to comment python code
How to write Pythonic code
How to learn python quickly
How to uninstall python plugin
How to understand python objects
How to use python tuples
How to practice after the python file is written
Python file read and write operations
python how to view webpage code
How to use python thread pool
Use C++ to write Python3 extensions
How to wrap in python code
How to save the python program
How to omit parentheses in Python
How to install Python 3.8 on CentOS 8
How to install Python 3.8 on Ubuntu 18.04
How to filter numbers in python
How to read Excel in Python
How to install Python on CentOS 8
How to solve python dict garbled
How to view errors in python
How Python operates on file directories
How to view the python module
How to understand variables in Python
How to clear variables in python
How to understand python object-oriented programming
How to use SQLite in Python
A practical guide to Python file handling
How to verify successful installation of python
How to make a globe with Python
How to use and and or in Python
How to introduce third-party modules in Python
How to represent null values in python
How to save text files in python
How to use PYTHON to crawl news articles
How to run id function in python
How to install third-party modules in Python
How to custom catch errors in python
How to define private attributes in Python
R&D: How To Install Python 3 on CentOS 7
How to add custom modules in Python
How to process excel table with python
How to understand global variables in Python
How to view installed modules in python
How to install Python2 on Ubuntu20.04 ubuntu/focal64
How to debug python program using repr
How to learn the Python time module
How to open python in different systems
How to sort a dictionary in python
How to get started quickly with Python
How to enter python triple quotation marks