由於python的開發任務中需要用到讀取ini檔案,然後對configparser模組進行學習並記錄如下筆記(我使用的是3.7.3)。
pip install configparser
[section]
name = value
name:value
用= :來賦值
1.我們來建立乙個ini檔案;**如下:
# -*- coding: utf-8 -*-
import configparser
config = configparser.configparser(
)file
='e:/code/python/config/config.ini'
config.read(
file
)config.add_section(
'login'
)config.
set(
'login'
,'username'
,'harry'
)config.
set(
'login'
,'password'
,'88888'
)with
open
(file
,'w'
)as configfile:
config.write(configfile)
以上add_section是來增加section,set是用來新增加section下的鍵值。
2.我們如何讀取ini檔案:**如下:
# -*- coding: utf-8 -*-
import configparser
config = configparser.configparser(
)file
='e:/code/python/config/config.ini'
config.read(
file
)username = config.get(
'login'
,'username'
)password = config.get(
'login'
,'password'
)print
(username,password)
get是用來獲取section下的某個鍵值。
3.檢查ini檔案中是否有login檔案,**如下
# -*- coding: utf-8 -*-
import configparser
config = configparser.configparser(
)file
='e:/code/python/config/config.ini'
config.read(
file
)test = config.has_section(
'login'
)print
(test)
以上是has_section的用法;
4 以下是刪除 section下的鍵值,**如下
# -*- coding: utf-8 -*-
import configparser
config = configparser.configparser(
)file
='e:/code/python/config/config.ini'
config.read(
file
)config.remove_option(
'login'
,'username'
)with
open
(file
,'w'
)as configfile:
config.write(configfile)
用的是remove_option的用法
5.建立乙個類,**如下:
import configparser
class
configoverwrite
(configparser.configparser)
:def
__init__
(self,defaults=
none):
configparser.configparser.__init__(self,defaults=
none
)def
optionxform
(self, optionstr)
:return optionstr
python3中異常處理 Python3異常處理
python的異常處理機制 使用 try.except 捕獲異常 try 業務實現 except error1,error2,as e 出現異常後的處理 異常類的繼承關係 baseexception systemexit keyboardinterrupt generatorexit excepti...
python3怎麼賦值 python3中賦值問題?
我閒著沒事乾來詳細回答一波。phthon的物件實際儲存在記憶體上,而變數名對應了乙個位址,位址指向了那一塊記憶體。在第一例中,python在記憶體中開出了一片用來儲存int值1,然後將它的位址賦值給a,接下來a把位址賦值給b。此時a,b指向同乙個int值物件。後來b 1的操作做的是先計算b 1,計算...
python3中的字典
字典是一種對映,eng2sp dict print eng2sp eng2sp one undo print eng2sp eng2sp two df print eng2sp d eng2sp one print d 字典中的順序是不可預料的 a len eng2sp 顯示鍵值對的個數 print...