iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python中ini配置文件读写的实现
  • 357
分享到

Python中ini配置文件读写的实现

2024-04-02 19:04:59 357人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

导入模块 import configparser # py3 写入 config = configparser.ConfigParser() config["DEFAULT"]

导入模块

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 parser
topsecret['ForwardX11'] = 'no'  # same here

config['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'])
# hg

print(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 = 45
compression = yes
compressionlevel = 9
forwardx11 = yes

[bitbucket.org]
user = hg

[topsecret.server.com]
host port = 50022
forwardx11 = 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=<object object at 0x0000000002F42120>)
     |      Get an option value for a given section.
     |  
     |  getboolean(self, section, option, *, raw=False, vars=None, fallback=<object object at 0x0000000002F42120>)
     |  
     |  getfloat(self, section, option, *, raw=False, vars=None, fallback=<object object at 0x0000000002F42120>)
     |  
     |  getint(self, section, option, *, raw=False, vars=None, fallback=<object object at 0x0000000002F42120>)
     |  
     |  has_option(self, section, option)
     |      Check for the existence of a given option in a given section.
     |      If the specified `section' is None or an empty string, DEFAULT is
     |      assumed. If the specified `section' does not exist, returns False.
     |  
     |  has_section(self, section)
     |      Indicate whether the named section is present in the configuration.

     |  items(self, section=<object object at 0x0000000002F42120>, raw=False, vars=None)
     |      Return a list of (name, value) tuples for each option in a section.
     |  
     |  options(self, section)
     |      Return a list of option names for the given section name.
     |  popitem(self)
     |      Remove a section from the parser and return it as
     |  read(self, filenames, encoding=None)
     |      Read and parse a filename or a list of filenames.
     |      Return list of successfully read files.
     |  
     |  read_dict(self, dictionary, source='<dict>')
     |      Read configuration from a dictionary.
     |  
     |  read_file(self, f, source=None)
     |      Like read() but the argument must be a file-like object.
     |      
     |  read_string(self, string, source='<string>')
     |      Read configuration from a given string.
     |  
     |  readfp(self, fp, filename=None)
     |      Deprecated, use read_file instead.
     |  
     |  remove_option(self, section, option)
     |      Remove an option.
     |  
     |  remove_section(self, section)
     |      Remove a file section.
     |  
     |  sections(self)
     |      Return a list of section names, excluding [DEFAULT]
     |  
     |  write(self, fp, space_around_delimiters=True)
     |      Write an .ini-fORMat representation of the configuration state.
     |  
     |  clear(self)
     |      D.clear() -> None.  Remove all items from D.
     |  
     |  pop(self, key, default=<object object at 0x0000000002F42040>)
     |      D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
     |      If key is not found, d is returned if given, otherwise KeyError is raised.
     |  
     |  setdefault(self, key, default=None)
     |      D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D
     |  
     |  update(*args, **kwds)
     |      D.update([E, ]**F) -> None.  Update D from mapping/iterable E and F.
     |      If E present and has a .keys() method, does:     for k in E: D[k] = E[k]
     |      If E present and lacks .keys() method, does:     for (k, v) in E: D[k] = v
     |      In either case, this is followed by: for k, v in F.items(): D[k] = v
     |  
     |  keys(self)
     |      D.keys() -> a set-like object providing a view on D's keys
     |  
     |  values(self)
     |      D.values() -> an object providing a view on D's values
     |  
"""

到此这篇关于python中ini配置文件读写的实现的文章就介绍到这了,更多相关Python ini文件读写内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Python中ini配置文件读写的实现

本文链接: https://www.lsjlt.com/news/139552.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python中ini配置文件读写的实现
    导入模块 import configparser # py3 写入 config = configparser.ConfigParser() config["DEFAULT"] ...
    99+
    2022-11-13
  • 如何实现Python中ini配置文件读写操作
    这篇文章将为大家详细讲解有关如何实现Python中ini配置文件读写操作,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。导入模块import configparser # py...
    99+
    2023-06-29
  • python读取/写入配置文件ini方法
    在写测试脚本时,经常有一些需要变动的数据,可以单独放在ini文件里,然后读取传递给 相应的函数,这样程序操作更灵活。具体的方法介绍如下: 文件结构: Cofig.ini内容:[test1]ip = 10.10.10.10 [test2]po...
    99+
    2023-01-31
    配置文件 方法 python
  • QT中怎么读写ini配置文件
    这篇文章主要介绍“QT中怎么读写ini配置文件”,在日常操作中,相信很多人在QT中怎么读写ini配置文件问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”QT中怎么读写ini配置文件”的疑惑有所帮助!接下来,请跟...
    99+
    2023-06-21
  • QT中如何读写ini配置文件
    如图1所示,我们需要在QT界面中实现手动读取参数存放的位置,那么我们该如何做呢? 方法:读取ini格式的配置文件,实现路径的写入与读取。 第一步:界面构造函数中,初始化一个Conf...
    99+
    2022-11-12
  • C++实现读写ini配置文件的示例代码
    目录1.概述2.ini格式语法3.配置读取4.demo示例5.自动生成读取代码1.概述 配置文件的读取是每个程序必备的功能,配置文件的格式多种多样,例如:ini格式、json格式、x...
    99+
    2023-05-19
    C++读写ini配置文件 C++读写ini文件 C++ ini文件
  • Python常用配置文件ini、json、yaml读写总结
    本文参考文章,出于学习目的,写本文。 开发项目时,为了维护一些经常需要变更的数据,比如数据库的连接信息、请求的url、测试数据等,需要将这些数据写入配置文件,将数据和代码分离,只需要...
    99+
    2022-11-12
  • 通过python读取ini配置文件
    ini是啥你可以理解为就是一个配置文件的统称吧。比如test.conf,这样的你可以理解为他就是ini文件,里面一般存放一些配置信息。比如数据库的基本信息,一会我们进行讲解!那么ta的好处是啥呢?就是把一些配置信息提出去来进行单独管理,如果...
    99+
    2023-01-31
    配置文件 python ini
  • python如何读取ini配置文件
    Python提供了一个标准库`configparser`用于读取和修改INI文件。首先,需要导入`configparser`模块:`...
    99+
    2023-10-08
    python
  • C语言读取写入ini配置文件的方法实现
    目录一、了解什么是INI文件?二、INI文件的格式三、解析上述文件四、测试如下一、了解什么是INI文件? ini 文件是Initialization File的缩写,即初始化文件,这...
    99+
    2022-11-12
  • java 读写 ini 配置文件的示例代码
    下面通过代码先看下java 读写 ini 配置文件,代码如下所示: package org.fh.util; import java.io.BufferedReader; impo...
    99+
    2022-11-12
  • Python配置文件管理之ini和yaml文件读取的实现
    1. 引言 当我们设计软件时,我们通常会花费大量精力来编写高质量的代码。但这往往还不够,一个好的软件还应该考虑其整个系统,如测试、部署、网络等。其中最重要的一个方面是配置管理。 良好...
    99+
    2023-02-28
    Python ini文件读取 Python ini和yaml文件读取
  • Python使用自带的ConfigParser模块读写ini配置文件
    在用Python做开发的时候经常会用到数据库或者其他需要动态配置的东西,硬编码在里面每次去改会很麻烦。Python自带有读取配置文件的模块ConfigParser,使用起来非常方便。 ini文件 ini配置...
    99+
    2022-06-04
    自带 配置文件 模块
  • Python的ini配置文件怎么写入
    本文小编为大家详细介绍“Python的ini配置文件怎么写入”,内容详细,步骤清晰,细节处理妥当,希望这篇“Python的ini配置文件怎么写入”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。INI介绍INI是英文...
    99+
    2023-06-29
  • C#怎么实现读写ini文件
    这篇文章主要介绍了C#怎么实现读写ini文件,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。1.创建一个C#项目2.创建一个ini文件在Debug目录下创建一个ini文件,写入...
    99+
    2023-06-28
  • java读写ini配置文件的示例代码怎么编写
    本篇文章为大家展示了java读写ini配置文件的示例代码怎么编写,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。下面通过代码先看下java 读写 ini 配置文件,代码如下所示:package&nbs...
    99+
    2023-06-26
  • Shell脚本读取ini配置文件的实现方法
    本篇内容介绍了“Shell脚本读取ini配置文件的实现方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!一、简单版参考stackoverfl...
    99+
    2023-06-09
  • Shell实现读取ini格式配置文件方法
    ini文件格式一般都是由节、键、值三部分组成 格式: [第一节 ] 第一个键 = 值 第二个键 = 第二个值 [第二节 ] 第一个键 = val1,val2,val3 例子: [COM] KINGGOO...
    99+
    2022-06-04
    配置文件 格式 方法
  • C++实现ini文件读写的示例代码
    目录介绍1.使用INIReader.h头文件1.INIReader.h2.test.ini3.INIReaderTest.cpp2.使用ini.h头文件1.ini.h2.config...
    99+
    2022-11-13
  • C语言Iniparser库实现ini文件读写
    目录一、概述二、使用下载方式一方式二三、API函数四、演示一、概述 iniparser是针对INI文件的解析器。ini文件则是一些系统或者软件的配置文件。iniparser库的API...
    99+
    2023-03-20
    C语言 ini文件读写 C语言 ini文件 C语言 Iniparser库
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作