python~Configparser模块总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_37579123/article/details/85165201

Configparser模块:读写配置文件

安装环境:py -3 -m pip install configparser

读配置文件

Config.ini配置文件内容:

[email]
mail_server=127.0.0.1
mail_username=yuzg
mail_password=123456
mail_receiver=test
mail_number=5

[path]
path=1

具体操作:

import os

import configparser

"获取文件的当前路径"

current_path=os.path.dirname(os.path.realpath(__file__))

#current_path=os.getcwd() 这个在不同的模块下运行会出错

#print (current_path)

"获取config.ini的路径"

config_path=os.path.join(current_path,"config.ini")

#print (config_path)


conf=configparser.ConfigParser()

conf.read(config_path)

#conf.read("config.ini")

conf.sections()

"读取所有sections,返回可用的section的列表;默认section不包括在列表中"

print (conf.sections())

for section in conf.sections():

     print (section)

#返回结果:
['email', 'path']

conf.has_section(section)

"判断指定的section是否出现在配置中"

if conf.has_section("email"):

    print ("配置文件中包含email部分")

else:

print ("配置文件中不包含email部分")

#返回结果:
配置文件中包含email部分

conf.options(section)

"读取所有options,返回指定section中可用的选项列表"

print (conf.options("email"))

for option in conf.options("email"):

    print (option)


#返回结果:
['mail_server', 'mail_username', 'mail_password', 'mail_receiver', 'mail_number']
mail_server
mail_username
mail_password
mail_receiver
mail_number

conf.has_option(section,option)

"如果给定的section存在,并且包含给定的选项,则返回True;否则返回False"

if conf.has_option("email","test"):

    print ("email部分有test选项")

else:

    print ("email部分没有test选项")

#返回结果为:
email部分没有test选项

"像字典一样取值"

print (conf["email"]["mail_server"])

#返回结果为:
127.0.0.1

conf.get(section,option)

"get用法:conf.get(section,option),返回类型为字符串类型"

mail_server=conf.get("email","mail_server")

print (mail_server)

mail_username=conf.get("email","mail_username")

print (type(mail_username))

mail_password=conf.get("email","mail_password")

print (mail_password)

mail_receiver=conf.get("email","mail_username")

print (mail_receiver)

path=conf.get("path","path")

print (type(path))

#返回结果为:
127.0.0.1
<class 'str'>
123456
yuzg
<class 'str'>

conf.getint(section,option)

"将指定section中的选项强制转换为整数,或者int直接转换"

mail_server=conf.get("email","mail_number")

print (type(mail_server))

mail_server=int(mail_server)

print (type(mail_server))


mail_server=conf.getint("email","mail_number")

print (type(mail_server))

#返回结果为:
<class 'str'>
<class 'int'>
<class 'int'>

conf.getfloat(section,option)

"指定section中的选项强制转换为浮点型"

mail_server=conf.get("email","mail_number")

print (type(mail_server))

mail_server=float(mail_server)

print (type(mail_server))


mail_server=conf.getfloat("email","mail_number")

print (type(mail_server))

#返回结果为:
<class 'str'>
<class 'float'>
<class 'float'>

conf.getboolean(section,option)

"强制转换为布尔型,”1”, “yes”, “true”, and “on”, 转换为True,”0”, “no”, “false”, and “off”, 转换为False,其他返回ValueError."

path=conf.getboolean("path","path")

print (path)

#返回结果:
True

conf.items(section)

"返回给定section中每个选项的(name,value)对的列表"

print (conf.items("email"))

print (type(conf.items("email")))

print (conf.items("email")[1])

#返回结果:
[('mail_server', '127.0.0.1'), ('mail_username', 'yuzg'), ('mail_password', '123456'), ('mail_receiver', 'test'), ('mail_number', '5')]
<class 'list'>
('mail_username', 'yuzg')

写配置文件

“思路:导入模块;实例化;添加部分;添加选项和内容”

import configparser


config=configparser.ConfigParser()

config.add_section("TEST")

config.set("TEST","test1","1210")

config.set("TEST","test2","1211")


with open("test.ini","w") as fp:

    config.write(fp)


#返回结果:
[TEST]
test1 = 1210
test2 = 1211

[TEST]
test1 = 1210

猜你喜欢

转载自blog.csdn.net/weixin_37579123/article/details/85165201
今日推荐