ini檔案處理

2021-09-20 18:53:41 字數 1547 閱讀 8079

import configparser

cfg = configparser.configparser()

read_ok=cfg.read('e:/test/mysql.ini') #操作之前必須先 read到記憶體中

print(read_ok)

print(cfg.sections())

print(cfg.default_section)

print(cfg._sections)

for k,v in cfg.items('mysqld'):

print(k,v)

x = cfg.get('mysqld','a')

print(x,type(x))

y = cfg.getint('mysqld','port')

print(y,type(y))

cfg.set('mysqld','c','true')

x = cfg.get('mysqld','c')

print(x,type(x))

嘗試讀取和解析檔名列表,返回已成功解析的檔名列表。如果檔名是字串,則將其視為單個檔名。如果無法開啟檔名中命名的檔案,該檔案將被忽略。這是為了能夠指定潛在的配置檔案位置列表(例如,當前目錄,使用者的主目錄和一些系統範圍的目錄),並且將讀取列表中的所有現有配置檔案。

把所有配置項讀取到記憶體中,用記憶體資料結構,進行讀寫操作,並不持久化.需要自行寫入檔案

with open('e:/test/test.ini','w') as f:

cfg.write(f)

print(cfg['mysqld']['port'])

print(type(cfg['mysqld']['port']))

cfg.add_section('test')

with open('e:/test/test.ini','w') as f:

cfg.write(f)

if cfg.has_section('test'):

cfg.remove_section('test')

with open('e:/test/test3.ini','w') as f:

cfg.write(f)

cfg['test3'] = {}

cfg['test3']['test'] = '123'

with open('e:/test/test3.ini','w') as f:

cfg.write(f)

if cfg.has_option('test3','test'):

cfg.remove_option('test3','test')

with open('e:/test/test3.ini','w') as f:

cfg.write(f)

print('test' in cfg['test3'])

常見檔案處理之INI檔案

ini檔案的結構如下 小節名 關鍵字 值 usesinifiles 常用的方法 writestring 寫入字串 writeinteger 寫入數字 writebool 寫入布林值 readstring 讀取字串值 readinteger 讀取數字 readbool 讀取布林值 readsectio...

GO系列 ini檔案處理

gopkg.in ini.v1 建立乙個空的配置 cfg ini.empty 直接載入存在的配置檔案,如果檔案不存在就會報錯 可以同時載入多個配置檔案,後面的配置檔案鍵值會覆蓋前面乙個 不能夠確定其中哪些檔案是不存在的,可以通過呼叫函式 looseload 來忽略它們。跳過無法識別的資料行 cfg,...

18 csv檔案 ini檔案處理

目錄 2 ini檔案處理 2.3 字典的訪問方式 逗號分隔值 comma separated values,csv,有時也稱為字元分隔值,因為分隔字元也可以不是逗號 其檔案以純文字形式儲存 資料 數字和文字 下面都是csv檔案的內容格式 1,2,3,4,5 1,2,1,3 1,2,a,f 它有以下規...