使用 python 读取 ini 文件

read_ini.ini:

[baseconf]
host=127.0.0.1
port=3306
user=root
password=root
db_name=evaluting_sys

[concurrent]
processor=20

read_ini.py:

# -*- coding:utf-8 -*-
import ConfigParser

config = ConfigParser.ConfigParser()
config.read('read_ini.ini')

section_list = config.sections()
print(section_list)

option_list = config.options('baseconf')
print(option_list)

item_list = config.items('baseconf')
print(item_list)

db_host = config.get('baseconf', 'host')
print(db_host)

db_post = config.getint('baseconf', 'port')
print(db_post)

输出:

['baseconf', 'concurrent']
['host', 'port', 'user', 'password', 'db_name']
[('host', '127.0.0.1'), ('port', '3306'), ('user', 'root'), ('password', 'root'), ('db_name', 'evaluting_sys')]
127.0.0.1
3306

Process finished with exit code 0

你可能感兴趣的:(Python基础)