如何实现Python中ini配置文件读写操作
发表于:2025-02-07 作者:千家信息网编辑
千家信息网最后更新 2025年02月07日,这篇文章将为大家详细讲解有关如何实现Python中ini配置文件读写操作,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。导入模块import configparser
千家信息网最后更新 2025年02月07日如何实现Python中ini配置文件读写操作
这篇文章将为大家详细讲解有关如何实现Python中ini配置文件读写操作,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
导入模块
import configparser # py3
写入
config = configparser.ConfigParser()config["DEFAULT"] = { 'ServerAliveInterval': '45', 'Compression': 'yes', 'CompressionLevel': '9' }config['bitbucket.org'] = {}config['bitbucket.org']['User'] = 'hg'config['topsecret.server.com'] = {}topsecret = config['topsecret.server.com']topsecret['Host Port'] = '50022' # mutates the parsertopsecret['ForwardX11'] = 'no' # same hereconfig['DEFAULT']['ForwardX11'] = 'yes'# 写入文件with open('example.ini', 'w') as configfile: config.write(configfile)
读取
config = configparser.ConfigParser()config.read("example.ini")print(config.defaults())# OrderedDict([('compression', 'yes')])print(config.sections())# ['bitbucket.org', 'topsecret.server.com']print(config['bitbucket.org']['User'])# hgprint(config.options("topsecret.server.com"))# ['port', 'compression']print(config.items("topsecret.server.com"))# [('compression', 'yes'), ('port', '50022')]print(config.get("topsecret.server.com", "port"))# 50022
修改
print(config.has_section("Name"))# 删除config.remove_section("Name")# 添加config.add_section("Name")config["Name"]["name"] = "Tom"config["Name"]["asname"] = "Jimi"# 设置config.remove_option("Name", "asname")config.set("Name", "name", "Jack")# 保存config.write(open("example.ini", "w"))
附:ini文件
[DEFAULT]serveraliveinterval = 45compression = yescompressionlevel = 9forwardx11 = yes[bitbucket.org]user = hg[topsecret.server.com]host port = 50022forwardx11 = no
help(configparser)
"""CLASSES class ConfigParser(RawConfigParser) | ConfigParser implementing interpolation. | | add_section(self, section) | Create a new section in the configuration. Extends | RawConfigParser.add_section by validating if the section name is | a string. | | set(self, section, option, value=None) | Set an option. Extends RawConfigParser.set by validating type and | interpolation syntax on the value. | | defaults(self) | | get(self, section, option, *, raw=False, vars=None, fallback=
关于"如何实现Python中ini配置文件读写操作"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
文件
篇文章
配置
更多
不错
实用
内容
文章
模块
知识
D.
F.
参考
帮助
有关
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
数据库错误初始化
putty怎么退出数据库
二年级网络安全手抄报文字素材
国内常用数据库的优点
网络安全集中运营是指
浙江省网络安全检测评估
比较大的网络安全论坛或贴吧
沈与辛网络安全
敏捷网络技术
数据库系统中的空值是
学软件开发的具体执行计划
崇明区网络营销软件开发欢迎选购
PCL2 服务器皮肤 全一样
网络安全销售许可证
软件开发工具文档生成中
临猗县云嵩网络技术有限公司
bt服务器查询失败
虚拟机文件服务器
常德培训软件开发的机构
云服务器免费的
软件开发工程师的好处
ota数据库
阿里云服务器增加带宽
服务器名和计算机名一样吗
论文四大数据库都有哪些
丰台区信息网络技术服务哪家好
华为复制应用数据库
闪回 数据库
网络安全的六级翻译
小规模软件开发企业退税