.ini文件的python处理

  

复制代码
from configparser import ConfigParser

cfg=ConfigParser()
cfg.read('c:/mysql.ini')
print(cfg.sections())
print(cfg.has_section('client'))
print(cfg.items('mysqld'))
for k,v in cfg.items():
    print(k,v,type(v))

tmp=cfg.get('mysqld','port')
print(tmp,type(tmp))
print(cfg.get('mysqld','a'))
#print(cfg.get('mysqld','vbn'))
print(cfg.get('mysqld','vbn',fallback='bbbbbbbbbbbbbbbb'))
tmp=cfg.getint('mysqld','port')
print(tmp,type(tmp))

if cfg.has_section('jar'):
    cfg.remove_section('jar')

cfg.add_section('lag')
cfg.set('lag','lag1','1')
cfg.set('lag','lag2','zxc')

with open('mysql.ini','w') as f:
    cfg.write(f)

print(cfg.getint('lag','vbn'))
cfg.remove_option('lag','vbn')

with open('mysql.ini',mode='w') as f:
    cfg.write(f)
    
复制代码

 

复制代码
from configparser import ConfigParser

cfg=ConfigParser()
cfg.read('c:/z.ini')
print(cfg.sections())
print(cfg.has_section('jar'))
# print(cfg.items('mysqld'))
for section in cfg.sections():
    for option in cfg.options(section):
        print(section,option,type(option))
# for k,v in cfg.items('mysqld'):
#     print(k,type(v))

for k,v in cfg.items():
    print(k,v,type(v))

if not cfg.has_section('jar'):
    cfg.add_section('jar')

# cfg.set('jar','jar1','123')
# cfg.set('jar','jar2','abc')
# print(cfg.sections())
#
# with open('c:/z.ini',mode='wt+') as f:
#     cfg.write(f)

print(cfg.sections())
print(cfg.options('jar'))
print(type(cfg.get('jar','jar1')))
b=cfg.getint('jar','jar1')
print(b,type(b))
b=cfg.get('jar','a')
print(b)
print(cfg.get('mysqld','vbn',fallback='bbbbbbbbbbbbbbbbbbb'))
if cfg.has_section('jar'):
    cfg.remove_section('jar')
with open('c:/z.ini',mode='wt+') as f:
    cfg.write(f)
复制代码

 

posted @   ascertain  阅读(223)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示