Python:Python 使用 configparser 模块

2020-07-24  本文已影响0人  dex0423

1. configparser

2. 示例


 [db]
[db]
db_host = localhost
db_port = 3306
db_user = xxx
db_passwd = xxxxxx
db_name = xxx

[concurrent]
thread= 10

import configparser


cf = configparser.ConfigParser()
cf.read("unitConf.conf")

# return all section
secs = cf.sections()
print('sections:', secs)   # sections: ['db', 'concurrent']

opts = cf.options("db")
print('options:', opts)    # options: ['db_host', 'db_port', 'db_user', 'db_passwd']

kvs = cf.items("db")
print('db:', kvs)          # db: [('db_host', 'localhost'), ('db_port', '3306'), ('db_user', 'xxx'), ('db_passwd', 'xxxxxx')]

# read by type
host = cf.get("db", "db_host")
port = cf.get("db", "db_port")
dbname = cf.get("db", "db_name")
usernm = cf.get("db", "db_user")
passwd = cf.get("db", "db_passwd")

print(host)                # localhost
print(port)                # 3306
print(dbname)              # xxx
print(usernm)              # xxx
print(passwd)              # xxxxxx

上一篇下一篇

猜你喜欢

热点阅读