.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)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律