【Python模块】configparser模块-创新互联

configparser模块:

企业建站必须是能够以充分展现企业形象为主要目的,是企业文化与产品对外扩展宣传的重要窗口,一个合格的网站不仅仅能为公司带来巨大的互联网上的收集和信息发布平台,成都创新互联面向各种领域:成都OPP胶袋成都网站设计公司全网营销推广解决方案、网站设计等建站排名服务。

 是python标准库用来解析配置文件的模块。

格式:

    section:使用[]标记section名

    :或= :使用:或=赋值

[websv]
ip:'192.168.1.10'
port:443
name = 'root'
pw = 'root1990'
定义:
websv叫section

    同一个项可以多个值:

ip:'192.168.1.11','192.168.1.12','192.168.1.13'  #待测试

    read配置文件时,会自动把参数名变成小写

    一个section下有多个相同的参数,只能读取最后一个

方法、属性名参数作用示例
ConfigParser()

创建configpaser实例

read(filename)
filename: ini格式的文件名
打开ini格式的文件

sections()

以list形式返回所有section

items(section name)

section name:

指定section名字


把指定section的所有参数和值的元组,以list形式返回

options(section name)

section name:

指定section名字

以list形式,返回section里所有参数名
get[section name][args name]

section name:指定section名字

argsname:指定参数名

返回section的单个参数值。
getint()\getboolean()\getfloat()


add_section(section_name)section_name:指定section名字添加一个新的section
set(section_name,args_name,value)

section_name:指定section名字

args_name:指定参数名

value:设定参数的值

设定具体的参数值。
remove_section(section_name)section_name:指定section名字删除指定的section
remove_option(section_name,args_name)

section_name:指定section名字

args_name:指定参数名

删除section中的args项
clear()
清空除DEAFULT外所有section
write(open(file_name,'w'))open(file_name,'w')):以写模式打开一个文件把以上的编辑完成的信息存到file_name
has_section(section name)section name:指定section

寻找配置文件中指定的section

找到返回True,找不到返回False

进阶操作:


单个参数值是多行除首行外,其它行加一个空格

args = “行1

 行2”

结果:

行1

行2

参数值带变量url = http://%(host)s:%(port)s/Portal

[web]

host = '192.168.0.1'

port = 8000

url = http://%(host)s:%(port)s/Portal

结果:

http://192.168.0.1':8000/Portal

'section name' in configparser实例

判断实例中是否有section

返回True或False

DEAFUALT只能用此方式判断


‘DEAFULT’只能用此方法判断




####例一:
import configparser

config = configparser.ConfigParser()
config.read('example.ini')

#section_name = ['webserver']
#args_name = ['ip', 'port', 'url']
config.add_section('webserver')
config.set('webserver','ip','192.168.0.1')
config.set('webserver','port',8000)

config.set('webserver','url','http//:%(ip)s:%(port)s')
config.write(open('example.ini','w'))

print(config.sections())
print(config.options())
print(config.items())
print(config.get['webserver']['url'])

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻名称:【Python模块】configparser模块-创新互联
标题路径:http://myzitong.com/article/dgiohd.html