>>>>>importconfigparser>>>>>>file=r'D:\test.ini'>>>file'D:\\test.ini'>>>cf=configparser.ConfigParser()>>>file_nam" />

欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

python configparser模塊

系統(tǒng) 2059 0

? ? ? ? python configparser模塊用來處理ini文件,讀、寫都很方便,唯一要注意的是ini文件有格式要求,格式為:鍵=值,如果沒有等于號,讀取時會拋出異常。

python configparser模塊_第1張圖片

一、讀取文件

1.1、read(filename),讀取ini文件內(nèi)容。

            
              >>> 
>>> 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相關(guān)內(nèi)容都全部刪除)。

3.2、remove_option(section,option),刪除文件中某個section下的option的數(shù)值。

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張圖片


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲国产一区视频 | 夜夜cao| 国产精品视频一区二区三区 | 久久国产一区 | 成人一级大片 | 久久福利在线 | a色在线 | 国产亚洲蜜芽精品久久 | 天堂色网站| 日日摸夜夜添夜夜 | 九九九色| 又黄又爽的网站 | 天天做天天爽 | 天天干天日| 日韩欧美在线视频播放 | 久久夜色精品国产亚洲噜噜 | 蜜桃精品导航 | 国产69精品久久久久999小说 | 色永久| 欧美成人精品二区三区99精品 | 伊人久久99亚洲精品久久频 | 婷婷玖玖 | 李旭个人资料及简介 | 中文字幕在线观看电影 | 青草视频在线观看免费资源 | 日韩视频在线一区二区三区 | 国产欧美日韩综合精品一区二区 | 亚洲欧美一区二区三区综合 | 日韩免费大片 | 人人搞人人干 | 成年人小视频网站 | 欧美 亚洲 一区 | 日韩精品成人a在线观看 | 日本高清不卡在线 | 九九综合九九 | 国产永久在线视频 | 婷婷97狠狠的狠狠的爱 | 天天操综合网 | 秋霞激情 | 亚洲国产综合人成综合网站00 | seku.tv|