首页 后端开发 Python教程 Python中的ConfigParser模块使用详解

Python中的ConfigParser模块使用详解

Jun 10, 2016 pm 03:13 PM
python

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将内容写入配置文件。

 

3.基本例子

test.conf
[sec_a] 
a_key1 = 20 
a_key2 = 10 
  
[sec_b] 
b_key1 = 121 
b_key2 = b_value2 
b_key3 = $r 
b_key4 = 127.0.0.1

登录后复制

parse_test_conf.py
import ConfigParser 
cf = ConfigParser.ConfigParser() 
#read config 
cf.read("test.conf") 
# return all section 
secs = cf.sections() 
print 'sections:', secs 
  
opts = cf.options("sec_a") 
print 'options:', opts 
  
kvs = cf.items("sec_a") 
print 'sec_a:', kvs 
  
#read by type 
str_val = cf.get("sec_a", "a_key1") 
int_val = cf.getint("sec_a", "a_key2") 
  
print "value for sec_a's a_key1:", str_val 
print "value for sec_a's a_key2:", int_val 
  
#write config 
#update value 
cf.set("sec_b", "b_key3", "new-$r") 
#set a new value 
cf.set("sec_b", "b_newkey", "new-value") 
#create a new section 
cf.add_section('a_new_section') 
cf.set('a_new_section', 'new_key', 'new_value') 
  
#write back to configure file 
cf.write(open("test.conf", "w"))

登录后复制

得到终端输出:

sections: ['sec_b', 'sec_a'] 
options: ['a_key1', 'a_key2'] 
sec_a: [('a_key1', "i'm value"), ('a_key2', '22')] 
value for sec_a's a_key1: i'm value 
value for sec_a's a_key2: 22
登录后复制

更新后的test.conf

[sec_b] 
b_newkey = new-value 
b_key4 = 127.0.0.1 
b_key1 = 121 
b_key2 = b_value2 
b_key3 = new-$r 
  
[sec_a] 
a_key1 = i'm value 
a_key2 = 22 
  
[a_new_section] 
new_key = new_value
登录后复制

4.Python的ConfigParser Module中定义了3个类对INI文件进行操作。分别是RawConfigParser、ConfigParser、SafeConfigParser。RawCnfigParser是最基础的INI文件读取类,ConfigParser、SafeConfigParser支持对%(value)s变量的解析。

设定配置文件test2.conf

[portal] 
url = http://%(host)s:%(port)s/Portal 
host = localhost 
port = 8080
登录后复制

使用RawConfigParser:

import ConfigParser 
 
cf = ConfigParser.RawConfigParser() 
 
print "use RawConfigParser() read" 
cf.read("test2.conf") 
print cf.get("portal", "url") 
 
print "use RawConfigParser() write" 
cf.set("portal", "url2", "%(host)s:%(port)s") 
print cf.get("portal", "url2")
登录后复制

得到终端输出:

use RawConfigParser() read 
http://%(host)s:%(port)s/Portal 
use RawConfigParser() write 
%(host)s:%(port)s
登录后复制

改用ConfigParser:

import ConfigParser 
 
cf = ConfigParser.ConfigParser() 
 
print "use ConfigParser() read" 
cf.read("test2.conf") 
print cf.get("portal", "url") 
 
print "use ConfigParser() write" 
cf.set("portal", "url2", "%(host)s:%(port)s") 
print cf.get("portal", "url2")
登录后复制

得到终端输出:

use ConfigParser() read 
http://localhost:8080/Portal 
use ConfigParser() write 
localhost:8080
登录后复制

改用SafeConfigParser:

import ConfigParser 
 
cf = ConfigParser.SafeConfigParser() 
 
print "use SafeConfigParser() read" 
cf.read("test2.conf") 
print cf.get("portal", "url") 
 
print "use SateConfigParser() write" 
cf.set("portal", "url2", "%(host)s:%(port)s") 
print cf.get("portal", "url2")
登录后复制

得到终端输出(效果同ConfigParser):

use SafeConfigParser() read 
http://localhost:8080/Portal 
use SateConfigParser() write 
localhost:8080
登录后复制

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

怎么下载deepseek 小米 怎么下载deepseek 小米 Feb 19, 2025 pm 05:27 PM

如何下载 DeepSeek 小米?在小米应用商店搜索“DeepSeek”,如未找到,则继续步骤 2。确定您的需求(搜索文件、数据分析),并找到包含 DeepSeek 功能的相应工具(如文件管理器、数据分析软件)。

deepseek怎么问他 deepseek怎么问他 Feb 19, 2025 pm 04:42 PM

有效使用DeepSeek的关键在于清晰提问:直接、具体地表达问题。提供具体细节和背景信息。对于复杂的询问,包含多个角度和反驳观点。关注特定方面,例如代码的性能瓶颈。对得到的答案保持批判性思维,结合专业知识进行判断。

deepseek该怎么搜索 deepseek该怎么搜索 Feb 19, 2025 pm 05:18 PM

直接使用DeepSeek自带的搜索功能即可,它强大的语义分析算法能准确理解搜索意图,提供相关信息。但对于冷门领域、最新信息或需要思考问题的搜索,需要调整关键词或使用更具体的描述、结合其他实时信息来源,并明白DeepSeek只是一个工具,需要主动、清晰、精细的搜索策略。

deepseek怎么编程 deepseek怎么编程 Feb 19, 2025 pm 05:36 PM

DeepSeek并非编程语言,而是深度搜索概念。实现DeepSeek需基于现有语言选择。针对不同应用场景,需要选择合适的语言和算法,并结合机器学习技术。代码质量、可维护性、测试至关重要。根据需求选择合适的编程语言、算法和工具,并编写高质量代码,才能成功实现DeepSeek。

deepseek怎么用来算账 deepseek怎么用来算账 Feb 19, 2025 pm 04:36 PM

问题:DeepSeek是否可用于会计?回答:不是,它是一个数据挖掘和分析工具,可用于分析财务数据,但本身不具备会计软件的账目记录和报表生成功能。使用DeepSeek分析财务数据需要:编写代码来处理数据具备对数据结构、算法和DeepSeek API的了解考虑潜在的问题(例如,编程知识、学习曲线、数据质量)

DeepSeekapi怎么接入-DeepSeekapi接入调用教程 DeepSeekapi怎么接入-DeepSeekapi接入调用教程 Mar 12, 2025 pm 12:24 PM

DeepSeekAPI接入与调用详解:快速上手指南本文将详细指导您如何接入和调用DeepSeekAPI,助您轻松使用强大的AI模型。第一步:获取API密钥访问DeepSeek官方网站,点击右上角的“开放平台”。您将获得一定数量的免费Tokens(用于计量API使用量)。在左侧菜单中,点击“APIKeys”,然后点击“创建APIkey”。为您的APIkey命名(例如,“test”),并立即复制生成的密钥。请务必妥善保存此密钥,因为它只会显示一次

Pi币重大更新:Pi Bank要来了! Pi币重大更新:Pi Bank要来了! Mar 03, 2025 pm 06:18 PM

PiNetwork即将推出革命性移动银行平台PiBank!PiNetwork今日发布重大更新Elmahrosa(Face)PIMISRBank,简称PiBank,它将传统银行服务与PiNetwork加密货币功能完美融合,实现法币与加密货币的原子交换(支持美元、欧元、印尼盾等法币与PiCoin、USDT、USDC等加密货币的互换)。究竟PiBank有何魅力?让我们一探究竟!PiBank主要功能:一站式管理银行账户和加密货币资产。支持实时交易,并采用生物特

当下ai切片工具有哪些 当下ai切片工具有哪些 Nov 29, 2024 am 10:40 AM

以下是一些流行的 AI 切片工具:TensorFlow DataSetPyTorch DataLoaderDaskCuPyscikit-imageOpenCVKeras ImageDataGenerator

See all articles