iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python configparser模块怎么使用
  • 822
分享到

Python configparser模块怎么使用

2023-07-04 20:07:10 822人浏览 薄情痞子

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

摘要

本文小编为大家详细介绍“python configparser模块怎么使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“Python configparser模块怎么使用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢

本文小编为大家详细介绍“python configparser模块怎么使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“Python configparser模块怎么使用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

1 configparser 使用get方式读取.ini配置文件的配置内容

(1)首先编写如下所示的env.ini配置文件

[server]ip=192.168.1.200port=22username=rootpassWord=root[personal]name=redrose2100city=nanjingGitHub=redrose2100.github.io

(2) 编写解析.ini配置文件的python代码

import configparserconfig=configparser.ConfigParser()config.read("env.ini","utf-8")print(config.get("server","ip"))print(config.get("personal","name"))

运行结果为:

192.168.1.200
redrose2100

2 使用数组下标的方式读取.ini配置文件的内容

env.ini的内容同上述1中的内容

通过数组下标的方式读取配置文件内容的代码如下:

import configparserconfig=configparser.ConfigParser()config.read("env.ini","utf-8")print(config["server"]["ip"])print(config["personal"]["name"])

执行结果如下:

192.168.1.200
redrose2100

3 使用configparser写配置文件

import configparserconfig=configparser.ConfigParser()config["server"]={    "ip":"192.138.1.200",    "port":22,    "username":"root",    "password":"root"}config["personal"]={    "name":"redrose2100",    "city":"nanjing"}with open("test.ini","w") as f:    config.write(f)

执行之后,在当前目录下会生成一个test.ini文件,其内容如下:

[server]ip = 192.138.1.200port = 22username = rootpassword = root[personal]name = redrose2100city = nanjing

4 configparser 对section常用的操作:

  • (1)has_section(section) 判断读取的config对象是否还有指定的section

  • (2)sections() 获取读取到的config对象的所有sections列表

  • (3)add_section(section) 给读取到的config对象增加一个section,注意此时增加的section只是在config对象中,并没有写入到ini配置文件中

  • (4)remove_section(section) 给读取到的config对象删除一个section

实例代码如下所示:

import configparserconfig=configparser.ConfigParser()config.read("env.ini","utf-8")print(config.has_section("server"))print(config.sections())config.add_section("kafka")print(config.sections())config.remove_section("kafka")print(config.sections())

运行结果如下:

import configparser

config=configparser.ConfigParser()
config.read("env.ini","utf-8")
print(config.has_section("server"))
print(config.sections())
config.add_section("kafka")
print(config.sections())
config.remove_section("kafka")
print(config.sections())

5 configparser对option常用的操作,如下代码演示:

import configparserconfig=configparser.ConfigParser()config.read("env.ini","utf-8")print(config.has_option("server","ip"))print(config.options("server"))config.set("server","test","test")print(config.options("server"))config.remove_option("server","test")print(config.options("server"))

执行结果为:

True
['ip', 'port', 'username', 'password']
['ip', 'port', 'username', 'password', 'test']
['ip', 'port', 'username', 'password']

6 configparser的对象可以类似字典一样使用,但是类型不是字典,代码演示如下:

import configparserconfig=configparser.ConfigParser()config.read("env.ini","utf-8")for key in config["server"].keys():    print(key)for key,value in config["server"].items():    print(key,value)for value in config["server"].values():    print(value)item=config["server"].popitem()print(type(item))print(item)port=config["server"].pop("port")print(port)for key,value in config["server"].items():    print(key,value)username=config["server"].get("username","no found")print(username)print(type(config["server"]))

运行结果如下:

ip
port
username
password
ip 192.168.1.200
port 22
username root
password root
192.168.1.200
22
root
root
<class 'tuple'>
('ip', '192.168.1.200')
22
username root
password root
root
<class 'configparser.SectionProxy'>

7 可以将获取的类型直接转换为期望的数据类型,可用的方法有:

  • getint

  • getboolean

  • getfloat

  • get

下面将配置文件更新如下内容:

[server]ip=192.168.1.200port=22username=rootpassword=rootis_linux=Trueprice=100.24[personal]name=redrose2100city=nanjinggithub=redrose2100.github.io

实例代码如下:

import configparserconfig=configparser.ConfigParser()config.read("env.ini","utf-8")ip=config["server"].get("ip")port=config["server"].getint("port")is_linux=config["server"].getboolean("is_linux")price=config["server"].getfloat("price")print(ip,type(ip))print(port,type(port))print(is_linux,type(is_linux))print(price,type(price))

运行结果如下:

192.168.1.200 <class 'str'>
22 <class 'int'>
True <class 'bool'>
100.24 <class 'float'>

8 configparser标准库对解析.conf文件与解析.ini文件的使用方法是完全一样的,下面只演示一部分:

创建一个env.conf文件,内容如下:

[server]ip=192.168.1.200port=22username=rootpassword=rootis_linux=Trueprice=100.24[personal]name=redrose2100city=nanjinggithub=redrose2100.github.io

编写如下代码:

import configparserconfig=configparser.ConfigParser()config.read("env.conf","utf-8")print(config.get("server","ip"))print(config.get("personal","name"))print(config["server"]["ip"])print(config["personal"]["name"])

运行结果如下:

192.168.1.200
redrose2100
192.168.1.200
redrose2100

读到这里,这篇“Python configparser模块怎么使用”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注编程网Python频道。

--结束END--

本文标题: Python configparser模块怎么使用

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

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

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

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

下载Word文档
猜你喜欢
  • Python configparser模块怎么使用
    本文小编为大家详细介绍“Python configparser模块怎么使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“Python configparser模块怎么使用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢...
    99+
    2023-07-04
  • Python的configparser模块怎么使用
    今天小编给大家分享一下Python的configparser模块怎么使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获...
    99+
    2024-04-02
  • python configparser模块
    configparser模块:用于生成和修改常见配置文档来看一下开源软件的常见文档格式如下[DEFAULT] ServerAliveInterval = 45 Compression = yes Co...
    99+
    2023-01-30
    模块 python configparser
  • Python中ConfigParser模块如何使用
    Python中ConfigParser模块如何使用,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并...
    99+
    2023-06-17
  • Python学习之configparser模块的使用详解
    目录1 configparser安装2 configparser简介3 表示方法4 configparser详细使用4.1 对象初始化4.2 获取所有的sections4.3 获取所...
    99+
    2023-01-28
    Python configparser模块使用 Python configparser模块 Python configparser
  • Python中ConfigParser模块示例详解
    目录1. 简介2. ini配置文件格式3. 读取ini文件3.1 初始化对象并读取文件3.2 获取并打印所有节点名称3.3 获取指定节点的所有key3.4 获取指定节点的键值对3.5...
    99+
    2023-01-15
    Python中ConfigParser模块 Python中ConfigParser
  • Python 中怎么利用ConfigParser解析配置模块
    这篇文章将为大家详细讲解有关Python 中怎么利用ConfigParser解析配置模块,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。基本的读取配置文件-read(filename) 直接读...
    99+
    2023-06-04
  • Python configparser模块的用法示例代码
    1 configparser 使用get方式读取.ini配置文件的配置内容 (1)首先编写如下所示的env.ini配置文件 [server] ip=192.168.1.200 por...
    99+
    2022-12-21
    Python configparser模块 Python configparser模块用法 Python configparser用法
  • Python怎么使用模块
    这篇文章给大家分享的是有关Python怎么使用模块的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。1.1 导入模块import 模块名form 模块名import 功能名form 模块名 import *impor...
    99+
    2023-06-22
  • Python JSON模块怎么使用
    本篇内容主要讲解“Python JSON模块怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Python JSON模块怎么使用”吧!1.dumps( )将Python数...
    99+
    2023-06-25
  • Python requests模块怎么使用
    本文小编为大家详细介绍“Python requests模块怎么使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“Python requests模块怎么使用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习...
    99+
    2023-07-05
  • python中os模块和sys模块怎么使用
    今天小编给大家分享一下python中os模块和sys模块怎么使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。一、os模块o...
    99+
    2023-07-05
  • 怎么使用Python模块os
    本篇内容主要讲解“怎么使用Python模块os”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么使用Python模块os”吧!os.getcwd()返回当前工作目录绝对路径Python ...
    99+
    2023-06-02
  • python怎么使用contextvars模块
    前记在Python3.7后官方库出现了contextvars模块, 它的主要功能就是可以为多线程以及asyncio生态添加上下文功能,即使程序在多个协程并发运行的情况下,也能调用到程序的上下文变量, 从而使我们的逻辑解耦.上下文,可以理解为...
    99+
    2023-05-14
    Python contextvars
  • Python webargs模块怎么使用
    今天小编给大家分享一下Python webargs模块怎么使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。web...
    99+
    2023-06-28
  • python parser模块怎么使用
    Python的parser模块是用来解析语法的工具,可以根据给定的语法规则将字符串解析为Python对象。在Python中,有两种常...
    99+
    2023-09-12
    python
  • python webbrowser模块怎么使用
    要使用Python的webbrowser模块,需要先导入该模块:```pythonimport webbrowser```然后可以使...
    99+
    2023-08-24
    python Webbrowser
  • python IPy模块怎么使用
    本文小编为大家详细介绍“python IPy模块怎么使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“python IPy模块怎么使用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。IPy模块包含IP类,可以方便...
    99+
    2023-06-28
  • 使用Python模块:struct模块
    Python没有提供直接的将用户定义的数据类型和文件IO关联起来的功能,但是它提供了struct库(是一个内置库)——我们可以以二进制模式来写这些数据(有趣的是,它真的是设计来讲文本数据写为缓存的) 1)bytes、str...
    99+
    2023-01-31
    模块 Python struct
  • os模块与fnmatch模块怎么在python中使用
    本篇文章为大家展示了 os模块与fnmatch模块怎么在python中使用,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。python可以做什么Python是一种编程语言,内置了许多有效的工具,Pyt...
    99+
    2023-06-08
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作