Python3处理配置文件

1、说明:
python3使用configparser模块来处理ini配置文件。

2、代码示例:
需要生成conf.ini配置文件如下:
[config]
v1 = 100
v2 = abc
v3 = true
v4 = 123.45

python代码:
import configparser
# 加载现有配置文件
conf = configparser.ConfigParser()
# 写入配置文件
conf.add_section(‘config‘) #添加section
# 添加值
conf.set(‘config‘, ‘v1‘, ‘100‘)
conf.set(‘config‘, ‘v2‘, ‘abc‘)
conf.set(‘config‘, ‘v3‘, ‘true‘)
conf.set(‘config‘, ‘v4‘, ‘123.45‘)
# 写入文件
with open(‘conf.ini‘, ‘w‘) as fw:
    conf.write(fw)

# 读取配置信息
v1 = conf.getint(‘config‘, ‘v1‘)
v2 = conf.get(‘config‘, ‘v2‘)
v3 = conf.getboolean(‘config‘, ‘v3‘)
v4 = conf.getfloat(‘config‘, ‘v4‘)
print(‘v1:‘, v1)
print(‘v2:‘, v2)
print(‘v3:‘, v3)
print(‘v4:‘, v4)

打开conf.ini文件检查内容

3、模块常用函数:
1)读取配置文件
read(filename) 直接读取ini文件内容
sections() 得到所有的section,并以列表的形式返回
options(section) 得到该section的所有option
items(section) 得到该section的所有键值对
get(section,option) 得到section中option的值,返回为string类型
getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。
2)写入配置文件
add_section(section) 添加一个新的section
set( section, option, value) 对section中的option进行设置,需要调用write将内容写入配置文件。

时间: 2024-12-30 12:26:44

Python3处理配置文件的相关文章

Linux VIM8 Python3 编辑器配置文件

Linux VIM8 Python3 编辑器配置文件 "---------------------简单配置------------------------- set nocompatible "关闭与vi的兼容模式 set number "显示行号 set nowrap "不自动折行 set showmatch "显示匹配的括号 set scrolloff=3 "距离顶部和底部3行" set encoding=utf-8 "编

python3实现配置文件差异对比脚本

应用场景:配置文件由于升级改动了,我们想看看升级后的配置文件相对于之前的改动了哪些配置项注意:这个脚本只能检测的配置文件是键值对的形式,就是key=value的形式我在网上找了好久没找到这一块的案例,大部分都是用一些difflib库做的可视化对比,所以自己尝试写了一个 # 该脚本实现两个配置文件中,新文件相对于旧文件的增删改的配置项输出功能 # 配置文件必须是key = value的形式 import re import sys def data2list(file_stream): "&quo

linux安装python3环境并配置虚拟环境

1.安装必要库 yum -y install gcc yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel 2.下载python源码:https://www.python.org/downloads/source/ # 解压源码 ta

mac安装python3

1.mac 环境下安装 python3 1.查看 mac 自带系统版本 #查看系统自带的python open /System/Library/Frameworks/Python.framework/Versions #系统当前的python版本. python -V 2.开始安装(这里我们使用神器homebrew) #安装前先搜索一下是否已经存在python3的包: brew search python3 #已经存在,我们可以直接安装了: brew install python3 #出现如下报

python之模块配置文件ConfigParser(在python3中变化较大)

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块ConfigParser(在python3中为configparser) #特别注意:python3和python2关于该模块的功能用法有很大的不同. #配置文件解析器 import ConfigParser,os #初始化一个配置文件对象 config=ConfigParser.ConfigParser() #增加一个section config.add_section('Sectio

python3读取ini配置文件

python3读取ini配置文件(含中文)import configparser# 加载现有配置文件conn = configparser.ConfigParser()conn.read("KKD.ini", encoding="utf-8-sig") #此处是utf-8-sig,而不是utf-8 #以下两种方法读取文件内容效果一样print(conn.get('rclog', 'kkdqg_in')) 原文地址:https://www.cnblogs.com/te

python3以ftp方式备份华为交换机配置文件

客户这里,有很多华为S系列交换机,基本时都是2700,5700系列.数量很多,原来都是手工登陆备份,费时,费力.后来想用python脚本备份交换机配置文件.思路:1.华为交换机的配置文件都是以vrpcfg.zip文件方式保存在交换机内存中2.华为的交换机都支持ftp服务器3.使用python3脚本批量备份保存在windows主机指定目录4.有些设备可能故障等原因,无法进行备份,需要记录失败日志 前提条件,windows上已经安装好python3.6,配置好环境变量,脚本如下: #! env py

python3比较ini类型的配置文件方案

ini类型的配置文件有个特点,就是配置是分组的,每组有个section,section下面是键值对的形式,python3比较升级前和升级后的配置改变方案:第一步:将section和下面的键值对进行绑定file1 file2 列表,列表中每一项是每组section构成的字典section写成:tag=section名字的形式遍历file2 列表中的每组的tag每次取到一组的tag就去file1 列表中去找file1列表可以先把tag的value先收集为一个列表只要取file2的的tag遍历是不是在

Python3.5 day3作业二:修改haproxy配置文件。

需求: 1.使python具体增删查的功能. haproxy的配置文件. global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 info defaults log global mode http timeout connect 5000ms timeout client 50000ms timeout server 50000ms option dontlognull listen stats :8888 sta