Configparser ini of Python Module

Source: Internet
Author: User

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/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.