惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - 西康的博客

mysql information_schema库中COLUMNS 和 TABLES表字段详解 python - queue 队列模块 python - 修改系统环境变量 python - threading 多进程模块 python - socketserver python - 反射 python - socket 基础 python 序列化 字典 异常处理 类的成员,类的特殊方法 elasticsearch RESTfull _cat api python - 面向对象编程基础知识 (进阶) python - 面向对象编程基础知识 python 文件操作 python re 模块 正则表达式 python logging 模块 python hashlib 模块 python sys 模块
python configparser 模块
西康的博客 · 2018-03-25 · via 博客园 - 西康的博客

configparser 模块是用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser。

示例配置文件

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
 
[test1.net.cn]
user = test
host = localhost
port = 8080
 
[top.net.cn]
test_port = 9090
user = root
port = 2202

1、生成配置文件

import configparser
config = configparser.ConfigParser()
config["DEFAULT"] = {
    'ServerAliveInterval':45,
    'Compression':'yes',
    'CompressionLevel':9
}
config['test1.net.cn'] = dict() # 使某一块配置等于一个字典
config['test1.net.cn']['user'] = 'test' #使用键值对的方式添加一行配置
config['test1.net.cn']['host'] = 'localhost'
config['test1.net.cn']['port'] = '8080' #所有的值都以字符串的方式传进去

config['top.net.cn'] = dict() 
top = config['top.net.cn']
top['test_port'] = '9090'
top['user'] = 'root'
top['port'] = '2202'

with open('test.ini','w') as configfile:
    config.write(configfile) # 最后一定要写入到配置文件

#####################

[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9


[test1.net.cn]
user = test
host = localhost
port = 8080


[top.net.cn]
test_port = 9090
user = root
port = 2202

#####################

2、读取配置文件

import configparser
config = configparser.ConfigParser()
config.read('test.ini')


print ('top.net.cn' in config) # 判断一个sections是否在配置文件中
for key in config: # 遍历所有的sections
    print (key)
print (config.sections()) # 直接打印所有的sections
print (config.defaults()) # 获取DEFAULT的特殊方法,获取那个单独的sections,都会带有DEFALUT的值。
print (config['test1.net.cn']['port']) # 获取单独的某一个记录
print (config.items('top.net.cn')) # 获取一个sections的记录
print (config.options('test1.net.cn')) # 获取test1.net.cn的sections,其中也包括DEFALUT的值
print (config.items('top.net.cn')) #获取top.net.cn的所有记录
var = config.get('top.net.cn','test_port') #获取某一个sections下的一个key的值
print (var)

# True
# DEFAULT
# test1.net.cn
# top.net.cn
# ['test1.net.cn', 'top.net.cn']
# OrderedDict([('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9')])
# 8080
# [('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('test_port', '9090'), ('user', 'root'), ('port', '2202')]
# ['user', 'host', 'port', 'serveraliveinterval', 'compression', 'compressionlevel']
# [('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('test_port', '9090'), ('user', 'root'), ('port', '2202')]
# 9090

3、修改配置文件

import configparser
config = configparser.ConfigParser()
config.read('test.ini')

# 删除已经存在的sections top.net.cn。
remove_sections = config.remove_section('top.net.cn')

# 如果一个sections存在,再次添加就会出现异常。所以我现在先删除,然后再添加。
# add_section = config.has_section('leslie')
config.remove_section('leslie')

# 添加一个不存在的sections。
add_section = config.add_section('leslie')

# 对已经存在的sections中添加记录。
config.set('leslie','name','xikang')

# 删除已经在sections中的一条记录。
config.remove_option('test1.net.cn','port')

# 最后一定要把这些修改写到文件中。
config.write(open('test.ini','w'))