python configparser模块

系统 2043 0

        python configparser模块用来处理ini文件,读、写都很方便,唯一要注意的是ini文件有格式要求,格式为:键=值,如果没有等于号,读取时会抛出异常。

python configparser模块_第1张图片

一、读取文件

1.1、read(filename),读取ini文件内容。

            
              >>> 
>>> import configparser
>>> 
>>> file = r'D:\test.ini'
>>> file
'D:\\test.ini'
>>> cf = configparser.ConfigParser()
>>> file_name = cf.read(file)
Traceback (most recent call last):
  File "
              
                ", line 1, in 
                
                  
    file_name = cf.read(file)
  File "C:\python37\lib\configparser.py", line 696, in read
    self._read(fp, filename)
  File "C:\python37\lib\configparser.py", line 1014, in _read
    for lineno, line in enumerate(fp, start=1):
UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 15: illegal multibyte sequence
>>> file_name = cf.read(file, encoding='utf-8')
>>> file_name
['D:\\test.ini']
>>> 
                
              
            
          

1.2、sections(),读取所有的section,以列表形式返回。

            
              >>> 
>>> section = cf.sections()
>>> section
['xiaoming', 'liqin', 'xiaoqiang']
>>> 
            
          

1.3、options(sections),读取指定sections下所有options ,以列表形式返回。

            
              >>> 
>>> key = cf.options(section[1])
>>> key
['gradd', 'age', 'score', 'sex', 'registered']
>>> 
            
          

1.4、items(sections),读取指定section下所有的键值对,返回列表,列表里面是键值对元祖。

            
              >>> 
>>> item = cf.items(section[1])
>>> item
[('gradd', '2'), ('age', '7'), ('score', '89.5'), ('sex', 'female'), ('registered', 'false')]
>>> 
            
          

1.5、get(section, option),读取section中option的值,返回为string类型。

            
              >>> 
>>> sex = cf.get(section[1], key[3])
>>> sex
'female'
>>> 
            
          

1.6、getint(section,option),读取section中option的值,返回int类型。

            
              >>> 
>>> age = cf.getint(section[1], key[1])
>>> age
7
>>> 
>>> age = cf.get(section[1], key[1])
>>> age
'7'
>>> 
            
          

1.7、getfloat(section,option),读取section中option的值,返回float类型。

            
              >>> 
>>> score = cf.get(section[1], key[2])
>>> score
'89.5'
>>> 
>>> score = cf.getfloat(section[1], key[2])
>>> score
89.5
>>> 
            
          

1.8、getboolean(section,option),读取section中option的值,返回boolean类型。

            
              >>> 
>>> registere = cf.get(section[1], key[4])
>>> registere
'false'
>>> 
>>> registere = cf.getboolean(section[1], key[4])
>>> registere
False
>>> 
            
          

二、写入文件:

2.1、add_section(section),添加section项。

2.2、set(section,option,value),给section项中新增写入键值对,修改也是用它。

2.3、write(filename),将configparser对象写入ini文件。

            
              >>> 
>>> cf.add_section('xiaohong')
>>> cf.set('xiaohong', 'grade' , '4')
>>> cf.set('xiaohong', 'age' , '10')
>>> cf.set('xiaohong', 'score' , '87.7')
>>> cf.set('xiaohong', 'sex' , 'female')
>>> cf.set('xiaohong', 'registered' , 'true')
>>> 
>>> with open(file, 'w+') as f:
	cf.write(f)

	
>>> 
            
          

python configparser模块_第2张图片

三、修改文件

3.1、remove_section(section),删除文件中指定的section(整个section相关内容都全部删除)。

3.2、remove_option(section,option),删除文件中某个section下的option的数值。

3.3、set(section,option,value),修改section项中键值对。

3.4、最后记得写入文件write(filename)。

            
              >>> cf.remove_section('xiaoming')
True
>>> cf.remove_option('liqin', 'age')
True
>>> with open(file, 'w+') as f:
	cf.write(f)

	
>>> cf.set('xiaohong', 'score' , '100')
>>> with open(file, 'w+') as f:
	cf.write(f)

	
>>> 
            
          

python configparser模块_第3张图片


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论