Python使用自带的ConfigParser模块读写ini配置文件
作者:j_hao104 发布时间:2022-04-01 00:07:01
标签:Python,配置文件
在用Python做开发的时候经常会用到数据库或者其他需要动态配置的东西,硬编码在里面每次去改会很麻烦。Python自带有读取配置文件的模块ConfigParser,使用起来非常方便。
ini文件
ini配置文件格式:
读取配置文件:
import ConfigParser
conf = ConfigParser.ConfigParser()
conf.read('dbconf.ini') # 文件路径
name = conf.get("section1", "name") # 获取指定section 的option值
print name
sex = conf.get("section1", "sex") # 获取section1 的sex值
print age
输出:
jhao
male
写入配置文件:
import ConfigParser
conf = ConfigParser.ConfigParser()
conf.read('dbconf.ini')
conf.set("section1", "name", "jhao104") # 修改指定section 的option
conf.set("section1", "age", "21") # 增加指定section 的option
conf.add_section("section3") # 增加section
conf.set("section3", "site", "oschina.net") # 给新增的section 写入option
conf.write(open('dbconf.ini', 'w'))
输出:
0
投稿
猜你喜欢
- 1、python内置的sqlite3模块,创建数据库中的表,并向表中插入数据,从表中取出所有行,以及输出行的数量。#!/usr/bin/en
- 本文实例讲述了Python开发微信公众平台的方法。分享给大家供大家参考,具体如下:这两天将之前基于微信公众平台的代码重构了下,基础功能以库的
- 一、前言关于什么是Dapper(详细入口),在此不做赘述;本文仅对Dapper在.Net Core中的使用作扼要说明,所陈代码以示例讲解为主
- 本文实例为大家分享了JavaScript实现切换多张图片的具体代码,供大家参考,具体内容如下循环切换图片HTML+CSS+JavaScrip
- 本文实例为大家分享了python接入微信聊天机器人的具体代码,供大家参考,具体内容如下1.安装库wxpy:pip install -U wx
- 先来思考一个问题,B站一个视频的弹幕最多会有多少?比较多的会有2000条吧,这么多数据,B站肯定是不会直接把弹幕和这个视频绑在一起的。也就是
- 收集和分发数据是网络管理的职责之一,而且必须确保这些数据的准确性和安全性。不管它们是什么操作系统,数据库服务器需要特殊的管理以保证操作上的安
- 这篇文章主要介绍了简单了解Python3 bytes和str类型的区别和联系,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的
- 井字棋简介井字棋又称三子棋,英文名为Tic Tac Toe。具体玩法为在一个3x3的棋盘上,一个玩家用X做棋子,另一个玩家用O做棋子,谁先在
- 安装tf2onnx以及onnxruntimepip install onnxruntimepip install tf2onnxtf 转为o
- 抛出的异常信息:异常详细信息: System.ArgumentOutOfRangeException: “ddlTotalCostDisco
- 前言:数据库的操作越来越成为整个应用的性能瓶颈了,这点对于 Web 应用尤其明显。关于数据库的性能,这并不只是 DBA 才需要担心的事,而这
- 1.原始数据是这样的2.脚本如下:import pandas as pddf = pd.read_excel(r'E:\untitl
- 前言lambda是表达式,用于创建匿名函数,可以和filter、map、reduce配合使用。本文环境Python3.7。一、lambda表
- 目录为什么需要分区?分区的策略分区隐患为什么需要分区?当面对巨大的数据表的时候,至少有一件事情是确定的,表太大了以至于每次查询的时候我们没法
- 一. 开发前的准备1. 必须了解的知识 SDK:软件开发工具包,可以为开发者提供快速开发的工具沙箱环境:也就是测试环境支付宝支付金额的精度:
- Python中,队列是线程间最常用的交换数据的形式。Queue模块是提供队列操作的模块,虽然简单易用,但是不小心的话,还是会出现一些意外。创
- 当请求访问到某个视图时,我们想让它重定向到其他页面,应该怎么做呢?1.HttpResponseRedirect需求:当我们访问127.0.0
- 一、auth模块# 创建超级用户python manage.py createsuperuserfrom django.contrib im
- 本文实例讲述了Vue + Node.js + MongoDB图片上传组件实现图片预览和删除功能。分享给大家供大家参考,具体如下:公司要写一些