Reprinted from: http://hi.baidu.com/myitlyj/blog/item/25586bd7088ba3dba044df6b.html <br/> using configuration files in <SPAN class = 'wp _ keywordlink '> Programs </span> to flexibly configure some parameters is a very common thing, the parsing of configuration files is not complicated. This is especially true in Python. The officially released library contains the library for doing this, which is configparser. Here is a brief introduction. <Br/> the configuration file parsed by configparser is in the same format as the ini configuration file, which consists of multiple sections. Each section has multiple configuration items, for example: <br/> [dB] <br/> db_host = 127.0.0.1 <br/> db_port = 3306 <br/> db_user = root <br/> db_pass = PASSWORD <br/> [concurrent] <br/> thread = 10 <br/> processor = 20 <br/> assume that the preceding configuration file 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: <br/> #-*-encoding: gb2312-*-<br/> Import configparser <br/> Import string, OS, sys <br/> CF = configparser. configparser () <br/> CF. read ("test. conf ") <br/> # return all sections <br/> S = cf. sections () <br/> Print 'section: ', S <br/> O = cf. options ("DB") <br/> Print 'Options: ', O <br/> V = cf. items ("DB") <br/> Print 'db :', v <br/> Print '-' * 60 <br/> # It can be read by type. <br/> db_host = cf. get ("DB", "db_host") <br/> db_port = cf. getint ("DB", "db_port") <br/> db_user = cf. get ("DB", "db_user") <br/> db_pass = cf. get ("DB", "db_pass") <br/> # returns an integer. <br/> threads = cf. getint ("concurrent", "Thread") <br/> processors = cf. getint ("concurrent", "processor") <br/> Print "db_host:", db_host <br/> Print "db_port:", db_port <br/> Print "db_user: ", db_user <br/> Print" db_pass: ", db_pass <br/> Print" thread: ", threads <br/> Print" processor :", processors <br/> # modify a value and write it back. <br/> CF. set ("DB", "db_pass", "zhaowei") <br/> CF. write (open ("test. conf "," W ") <br/>