22.3-ini文件操作

作者: BeautifulSoulpy | 来源:发表于2019-10-12 21:49 被阅读0次

人的一生,应当像美丽的花,自己无所求,而却给人间以美!

在构建工程时,常常需要用到配置文件,用来配置项目的一些信息,比如数据库,请求网址,文件夹,线程、进程数等信息,这样就可以方便我们通过修改配置文件中的参数来很好地完成整个项目的功能修改或开发。配置文件一般区别于Python代码,会单独存放在一个文件夹中,方便管理,常见的配置文件格式有.conf, .ini, .yaml等。

配置文件本质还是 文本文件;

ini配置文件组成形式

ini是我们常见到的配置文件格式之一。

ini是微软Windows操作系统中的文件扩展名(也常用在其他系统)。

ini“初始化(Initial)”的缩写。正如该术语所表示的,INI文件被用来对操作系统或特定程序初始化或进行参数设置。

其基本组成形式如下:

  1. [ ]中括号里面的部分称为section,译为 节、区、段;
  2. 每一个section内,都是key=value形成的键值对,key称为 option选项;
  3. 注意这里的DEFAULT是缺省section的名字,必须大写
[section_1]
key1 = value1
key2 = value2
key3 = value3
key4 = value4

[section_2]
key1 = value1
key2 = value2
key3 = value3
key4 = value4

configparser

python3里面自带configparser模块来读取ini文件,python2的版本是Configparser

可以将section当做key,section存储着键值对组成的字典,可以把ini配置文件当做一个嵌套的字典。默认使用的是有序字典。

属性 -
sections() 返回section列表。缺省section不包括在内
_sections 直接拿回字典,不建议直接使用带下划线的方法
add_section(section_name) 增加一个section
has_section(section_name) 判断section是否存在
options(section) 返回section的所有option,会追加缺省section的option
has_option(section, option) 判断section是否存在这个option
get(section, option, *, raw=False, vars=None[, fallback]) 从指定的段的选项上取值,如果找到返回,如果没有找到就去找DEFAULT段有没有
- -
getint(section, option, *, raw=False, vars=None[, fallback]) 和get一样,返回指定类型数据
getfloat(section, option, *, raw=False, vars=None[, fallback])
getboolean(section, option, *, raw=False, vars=None[, fallback])
items(raw=False, vars=None) 没有section,则返回所有section名字及其对象
items(section, raw=False, vars=None) 如果指定section,则返回这个指定的section的键值对组成二元组。
- -
set(section, option, value) section存在的情况下,写入option=value,要求option、value必须是字符串。
remove_section(section) 移除section及其所有option
remove_option(section, option) 移除section下的option。
write(fileobject, space_around_delimiters=True) 将当前config的所有内容写入fileobject中,一般open函数使用w模式。
from configparser import ConfigParser

cfg = ConfigParser()
readok = cfg.read('./test.ini')
print(readok)
#-------------------------
['./test.ini']

print(cfg.sections())   #返回section列表。缺省section不包括在内。
print(cfg.default_section)
#-------------------------
['mysql', 'mysqld']
DEFAULT

# 取每个section部分的选项options() ;
for section in cfg.sections():
    print(cfg.options(section))
    print('~'*15)
#--------------------------------------------------------------
['default-character-set', 'a']
~~~~~~~~~~~~~~~
['datadir', 'port', 'character-set-server', 'sql_mode', 'a']
~~~~~~~~~~~~~~~

# 取每个option对应的值value;缺省字典放到最后;
for section in cfg.sections():
    print('~'*18)
    for option in cfg.options(section):
        print(option,cfg.get(section,option))
#--------------------------------------------
~~~~~~~~~~~~~~~~~~
default-character-set utf8
a test
~~~~~~~~~~~~~~~~~~
datadir /dbserver/data
port 33060
character-set-server utf8
sql_mode NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
a test

# get 与 getint \getfloat方法
port = cfg.get('mysqld','port')
print(type(port),port)

tmp = cfg.getint('mysqld','port')
print(type(tmp),tmp)
#---------------------------------------------
<class 'str'> 33060
<class 'int'> 33060


# item方法
print(cfg.items())
#---------------------------------
ItemsView(<configparser.ConfigParser object at 0x00000194FD0CB550>)

# 不指定item对象的section
for item in cfg.items():
    print(item)
#------------------------------------------
('DEFAULT', <Section: DEFAULT>)
('mysql', <Section: mysql>)
('mysqld', <Section: mysqld>)

# 指定item对象的section
for item in cfg.items(section):
    print(item)

('a', 'test')
('datadir', '/dbserver/data')
('port', '33060')
('character-set-server', 'utf8')
('sql_mode', 'NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES')

# item等价形式key,value;
for section in cfg.sections():
    for option,value in cfg.items(section):
        print(option,value)
    print('-'*14)
#-------------------------------------------
a 1000
default-character-set utf8
--------------
a test
datadir /dbserver/data
port 33060
character-set-server utf8
sql_mode NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
--------------
    
# ini文件写入落地;所有的改动都是在内存当中的,必须写入落地;
cfg['mysql']['b'] = '2000'
cfg['test3'] = {'c':'3000'}

with open('mysql.ini','w') as f:
    cfg.write(f)
--------------------------------------------------------
[DEFAULT]
a = test

[mysql]
default-character-set = utf8
a = 1000
b = 2000

[mysqld]
datadir = /dbserver/data
port = 33060
character-set-server = utf8
sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[test3]
c = 3000

相关文章

  • 22.3-ini文件操作

    人的一生,应当像美丽的花,自己无所求,而却给人间以美! 在构建工程时,常常需要用到配置文件,用来配置项目的一些信息...

  • 文件操作

    文件操作 目标 文件操作的作用 文件的基本操作打开读写关闭 文件备份 文件和文件夹的操作 一. 文件操作的作用 思...

  • 文件和目录处理相关

    文件和目录处理相关 题: 考点:文件操作/写入操作; 延伸:目录操作函数,其他文件操作; 文件读写操作 文件系统函...

  • 09-文件操作

    一、文件操作流程 a.普通文件操作流程: 打开文件 操作文件 关闭文件 b. json文件操作流程: open(文...

  • VBS文件操作

    VBS文件操作'操作文本文件,操作fso对象(文件对象操作) --------------------------...

  • 文件操作

    文件操作:打开文件、读写文件、操作文件内容 写入文件操作:(把大象装入冰箱)1.打开文件 ...

  • 类的补充

    一.复习 1.文件操作a.操作流程:打开文件(open),操作文件,关闭文件with open() as 文件变量...

  • 文件

    目标 文件操作的作用 文件的基本操作打开读写关闭 文件备份 文件和文件夹的操作 一. 文件操作的作用 思考:什么是...

  • 16总 正则表达式

    复习: 1.文件操作a.操作流程: 打开文件(open) --- 操作文件 --- 关闭文件(close)with...

  • 2018-09-10

    01-recode 1.文件操作a.操作流程:打开文件---》操作文件----》关闭文件with open() ...

网友评论

    本文标题:22.3-ini文件操作

    本文链接:https://www.haomeiwen.com/subject/bsecmctx.html