python配置檔案讀寫(ConfigParse)

2021-10-14 20:48:17 字數 4756 閱讀 3542

ini檔案格式:

[section0] 

key0 = value0

key1 = value1

[section1]

key2 = value2

key3 = value

示例

[config]

num =

40column =

8create_size_x =

400create_size_y =

300[color]

# 深藍

dark_blue =rgba(1,

77,103,

255)

# 淺藍

light_blue = rgba(96,

143,

159,

255)

# 亮黃

luminous_yellow = rgba(

251,

178,23,

255)

# 淺黃

light_yellow = rgba(

237,

222,

139,

255)

在python檔案中讀取config.ini引數

# coding:utf-8

import configparser # python3以後的安裝包改為小寫

config = configparser.configparser(

)config.read(

"../config.ini"

, encoding=

'utf-8'

)print

(config.sections())

# 列印所有section

print

(config.options(

"color"))

# 列印[color]這個section裡面所有options

print

(config.items(

"color"))

# 列印[color]這個section裡面所有鍵值對

print

(config.get(

"color"

,"dark_blue"))

print

(config.getint(

"config"

,"num"))

for sections in config.sections():

for items in config.items(sections)

:#列印單獨鍵值對

print

(items)

列印結果:

[

'config'

,'color'

,'db'

,'screw'

,'mode'][

'background_color'

,'light_blue'

,'purplish_blue'

,'dark_blue'

,'luminous_yellow'

,'light_yellow'

,'light_grey'

,'grey'

,'grey_80'

,'grey_60'

,'grey_40'

,'grey_20'

,'grey_10'

,'black'

,'black_80'

,'black_60'

,'black_40'

,'black_20'

,'black_10'

,'red'

,'red_80'

,'red_60'

,'red_40'

,'red_20'

,'red_10'][

('background_color'

,'rgba(134,145,153,255)'),

('light_blue'

,'rgba(96,143,159,255)'),

('purplish_blue'

,'rgba(3,38,58,255)'),

('dark_blue'

,'rgba(20,68,106,255)'),

('luminous_yellow'

,'rgba(222,125,44,255)'),

('light_yellow'

,'rgba(250,218,141,255)'),

('light_grey'

,'rgba(179,168,150,255)'),

('grey'

,'rgba(134,145,153,255)'),

('grey_80'

,'rgba(157,167,175,255)'),

('grey_60'

,'rgba(181,198,196,255)'),

('grey_40'

,'rgba(205,211,216,255)'),

('grey_20'

,'rgba(229,233,235,255)'),

('grey_10'

,'rgba(242,244,245,255)'),

('black'

,'rgba(0,0,0,255)'),

('black_80'

,'rgba(87,87,87,255)'),

('black_60'

,'rgba(134,134,133,255)'),

('black_40'

,'rgba(176,177,177,255)'),

('black_20'

,'rgba(217,217,217,255)'),

('black_10'

,'rgba(236,236,236,255)'),

('red'

,'rgba(185,0,15,255)'),

('red_80'

,'rgba(197,51,63,255)'),

('red_60'

,'rgba(211,102,111,255)'),

('red_40'

,'rgba(226,153,159,255)'),

('red_20'

,'rgba(240,204,207,255)'),

('red_10'

,'rgba(248,230,231,255)')]

rgba(20,

68,106,

255)40(

'num'

,'40')(

'column'

,'8')(

'create_size_x'

,'400')(

'create_size_y'

,'300')(

'background_color'

,'rgba(134,145,153,255)')(

'light_blue'

,'rgba(96,143,159,255)')(

'purplish_blue'

,'rgba(3,38,58,255)')(

'dark_blue'

,'rgba(20,68,106,255)')(

'luminous_yellow'

,'rgba(222,125,44,255)')(

'light_yellow'

,'rgba(250,218,141,255)'

)

#coding:utf-8

import configparser

config = configparser.configparser(

)config.read(

"../config.ini"

, encoding=

'utf-8')if

"mode"

notin config.sections():

config.add_section(

"mode"

)# 增加乙個section

config.

set(

"mode"

,"flag"

,"true"

)# 增加一條資料

with

open

("../config.ini"

,"w+"

)as f:

config.write(f)

配置檔案加入結果

[config]

num =

40column =

8create_size_x =

400create_size_y =

300[color]

# 深藍

dark_blue =rgba(1,

77,103,

255)

# 淺藍

light_blue = rgba(96,

143,

159,

255)

# 亮黃

luminous_yellow = rgba(

251,

178,23,

255)

# 淺黃

light_yellow = rgba(

237,

222,

139,

255)

[mode]

flag = true

python讀寫配置檔案

本文主要介紹在python中讀寫配置檔案。python主要使用configparser模組來進行配置檔案的操作。import configparser 讀操作 conf configparser.configparser conf.read config.ini 檔名 讀取指定的section和na...

Python之配置檔案讀寫

configparser模組 一 建立配置檔案 在d盤建立乙個配置檔案,名字為 test.ini 內容如下 baseconf host 127.0.0.1 port 3306 user root password root db name gloryroad test ip 127.0.0.1 in...

讀寫配置檔案

windows作業系統專門為此提供了6個api函式來對配置設定檔案進行讀 寫 getprivateprofileint 從私有初始化檔案獲取整型數值 getprivateprofilestring 從私有初始化檔案獲取字串型值 getprofileint 從win.ini 獲取整數值 getprof...