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

推荐订阅源

V
Vulnerabilities – Threatpost
U
Unit 42
F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
F
Full Disclosure
月光博客
月光博客
Engineering at Meta
Engineering at Meta
博客园_首页
The Register - Security
The Register - Security
G
Google Developers Blog
The Cloudflare Blog
博客园 - Franky
K
Kaspersky official blog
A
Arctic Wolf
Scott Helme
Scott Helme
C
Cisco Blogs
Hugging Face - Blog
Hugging Face - Blog
C
Check Point Blog
NISL@THU
NISL@THU
AI
AI
D
DataBreaches.Net
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
Vercel News
Vercel News
T
Tor Project blog
P
Privacy International News Feed
D
Docker
I
Intezer
L
LangChain Blog
P
Proofpoint News Feed
Security Latest
Security Latest
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
博客园 - 聂微东
AWS News Blog
AWS News Blog
Martin Fowler
Martin Fowler
P
Privacy & Cybersecurity Law Blog
V
V2EX
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
The Hacker News
The Hacker News
T
Tenable Blog
Blog — PlanetScale
Blog — PlanetScale
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog

博客园 - 西康的博客

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'))