如何写python的配置文件

作者:yang 时间:2022-08-06 15:57:09 

一、创建配置文件

在D盘建立一个配置文件,名字为:test.ini

内容如下:


[baseconf]
host=127.0.0.1
port=3306
user=root
password=root
db_name=gloryroad
[test]
ip=127.0.0.1
int=1
float=1.5
bool=True

注意:要将文件保存为ansi编码,utf-8编码会报错

文件中的[baseconf]为section

二、读配置文件

import ConfigParser

cf=ConfigParser.ConfigParser()

cf.read(path) 读配置文件(ini、conf)返回结果是列表

cf.sections() 获取读到的所有sections(域),返回列表类型

cf.options('sectionname') 某个域下的所有key,返回列表类型

cf.items('sectionname') 某个域下的所有key,value对

value=cf.get('sectionname','key') 获取某个yu下的key对应的value值

cf.type(value) 获取的value值的类型

(1)getint(section, option)

获取section中option的值,返回int类型数据,所以该函数只能读取int类型的值。

(2)getboolean(section, option)

获取section中option的值,返回布尔类型数据,所以该函数只能读取boolean类型的值。

(3)getfloat(section, option)

获取section中option的值,返回浮点类型数据,所以该函数只能读取浮点类型的值。

(4)has_option(section, option)

检测指定section下是否存在指定的option,如果存在返回True,否则返回False。

(5)has_section(section)

检测配置文件中是否存在指定的section,如果存在返回True,否则返回False。

三、动态写配置文件

cf.add_section('test') 添加一个域

cf.set('test3','key12','value12') 域下添加一个key value对

cf.write(open(path,'w')) 要使用'w'

learn to fail, failure to learn

内容扩展:

python使用配置文件过程

通过配置文件将变量暴露给用户修改

标准库模块configparser,从而可在配置文件中使用标准格式。

必须使用[files]、[colors]等标题将配置文件分成几部分(section)。标题的名称可随便指定,但必须将它们用方括号括起。


$ cat area.ini

[numbers]
pi: 3.1415926535893971

[messages]
greeting: Welcome to the area calutation program!
question: plse enter the radius
result_message: The area is

使用python 读取他


from configparser import ConfigParser
CONFIGFILE = "area.ini"

config = ConfigParser()
#读取配置文件
config.read(CONFIGFILE)

print(config['messages'].get('greeting'))

radius = float(input(config['messages'].get('question') + ' '))

# 以空格结束以便接着在当前行打印:
print(config['messages'].get('result_message'),end=' ')
print(config['numbers'].getfloat('pi') * radius**2)

来源:https://www.py.cn/faq/python/18539.html

标签:python,配置文件
0
投稿

猜你喜欢

  • Python Pandas知识点之缺失值处理详解

    2023-09-29 20:23:16
  • 分析python并发网络通信模型

    2023-12-15 11:13:59
  • Numpy(Pandas)删除全为零的列的方法

    2022-07-23 13:13:59
  • 前端优化,让你的网页显示的更快更流畅

    2009-06-08 13:09:00
  • Go语言中结构体方法副本传参与指针传参的区别介绍

    2024-05-05 09:29:45
  • Vue内部渲染视图的方法

    2024-04-28 09:19:57
  • R语言属性知识点总结及实例

    2022-06-28 04:39:02
  • mysql代码执行结构实例分析【顺序、分支、循环结构】

    2024-01-21 23:18:40
  • vue-quill-editor插入图片路径太长问题解决方法

    2024-05-29 22:46:29
  • python编写脚本之pyautogui的安装和使用教程

    2021-06-17 09:48:11
  • pip安装python库时报Failed building wheel for xxx错误的解决方法

    2021-12-12 04:55:53
  • 基于python内置函数与匿名函数详解

    2021-02-02 08:27:26
  • Python 字符串操作方法大全

    2023-12-06 04:21:48
  • SQLServer2005混合模式登录配置(用户登录错误18452,233,4064)

    2024-01-28 17:47:34
  • python dlib人脸识别代码实例

    2021-04-05 12:57:33
  • js版sliderBar(滑动条)控件

    2008-10-18 15:59:00
  • Python实现http接口自动化测试的示例代码

    2023-05-01 07:14:45
  • Python OpenCV之常用滤波器使用详解

    2023-02-07 22:18:25
  • 基于Bootstrap模态对话框只加载一次 remote 数据的解决方法

    2024-04-27 15:24:18
  • 一篇教程教你学会Python进制转换(十进制转二进制、八进制、十六进制)

    2022-10-04 06:56:04
  • asp之家 网络编程 m.aspxhome.com