python using a configuration file

By the configuration file variable exposed to the user to modify

Standard library modules configparser, enabling the use of a standard format in the configuration file.

You must be used [files], [colors] The title like profile into sections (section). The name of the title can be easily specified, 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

He read using python

from configparser import  ConfigParser
CONFIGFILE = "area.ini"

config = ConfigParser()
#读取配置文件
config.read(CONFIGFILE)

print(config['messages'].get('greeting'))

radius = float(input(config['messages'].get('question') + ' '))

# 以空格结束以便接着在当前行打印:
print(config['messages'].get('result_message'),end=' ')
print(config['numbers'].getfloat('pi') * radius**2)

Configuration or control the following three sources of information, where the order should you query these sources, so that the latter covering the front of the source of sources:
1, the profile
2, the environment variable
3, the command line and passed to the program switches parameters: command line parameters to be processed, may be used as the sys.argv; switch to be processed (option), the module should be used argparse

Guess you like

Origin www.cnblogs.com/g2thend/p/11813549.html