You can use the following articles to better understand the Python configuration file, I hope you will give a brief introduction to its simple application in computer language. The following articles will introduce it in detail.
It is common to use a configuration file in a program to flexibly configure some parameters. The parsing of configuration files is not complicated, especially in python, the officially released library contains the library for doing this, that is, ConfigParser. Here is a brief introduction. The format of the configuration file parsed by ConfigParser is similar to that of the ini configuration file, which consists of multiple sections. Each section has multiple configuration items, for example:
- [db]
- db_host=127.0.0.1
- db_port=3306
- db_user=root
- db_pass=password
- [concurrent]
- thread=10
- processor=20
Assume that the configuration file above is named test. conf. It contains two sections, one is db, the other is concurrent, the db also contains four items, and the concurrent contains two items. Here we will do the parsing:
- #-*- encoding: gb2312 -*-
- import ConfigParser
- import string, os, sys
- cf = ConfigParser.ConfigParser()
- cf.read("test.conf")
Returns all sections.
- s = cf.sections()
- print 'section:', s
- o = cf.options("db")
- print 'options:', o
- v = cf.items("db")
- print 'db:', v
- print '-'*60
Can be read by type
- db_host = cf.get("db", "db_host")
- db_port = cf.getint("db", "db_port")
- db_user = cf.get("db", "db_user")
- db_pass = cf.get("db", "db_pass")
The returned value is an integer. The above content is an introduction to the Python configuration file.