如何编写Python配置文件的详细步骤?

2026-05-25 03:401阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计712个文字,预计阅读时间需要3分钟。

如何编写Python配置文件的详细步骤?

创建配置文件+在磁盘上创建一个配置文件,命名为test.ini,内容如下:[baseconf]host=127.0.0.1port=3306user=rootpassword=rootdb_name=gloryroad[test]ip=127.0.0.1int=1float=1.5bool=True注意:要将文件保存为test.ini。

一、创建配置文件

在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值的类型

如何编写Python配置文件的详细步骤?

(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)

到此这篇关于如何写python的配置文件的文章就介绍到这了,更多相关python写配置文件方法内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

本文共计712个文字,预计阅读时间需要3分钟。

如何编写Python配置文件的详细步骤?

创建配置文件+在磁盘上创建一个配置文件,命名为test.ini,内容如下:[baseconf]host=127.0.0.1port=3306user=rootpassword=rootdb_name=gloryroad[test]ip=127.0.0.1int=1float=1.5bool=True注意:要将文件保存为test.ini。

一、创建配置文件

在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值的类型

如何编写Python配置文件的详细步骤?

(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)

到此这篇关于如何写python的配置文件的文章就介绍到这了,更多相关python写配置文件方法内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!