1. 기본 읽기 구성 파일
-read(filename)은 ini 파일 내용을 직접 읽어
-sections()는 모든 섹션을 가져와서 목록 형식으로 반환합니다
-options(section) 섹션의 모든 옵션을 가져옵니다
-items(section) 섹션의 모든 키-값 쌍을 가져옵니다
-get(section,option ) 섹션 가져오기 섹션의 옵션 값은 문자열 유형으로 반환됩니다.
-getint(section, option) 섹션의 옵션 값을 가져오고 int 유형으로 반환됩니다. 해당 getboolean() 및 getfloat() 함수.
2. 기본 쓰기 구성 파일
-add_section(section) 섹션의 옵션에 새로운 섹션
-set(section, option, value)을 추가하여 설정합니다. , 구성 파일에 콘텐츠를 쓰려면 write를 호출해야 합니다.
3. 기본 예시
test.conf
1 2 3 4 5 6 7 8 9 | [sec_a]
a_key1 = 20
a_key2 = 10
[sec_b]
b_key1 = 121
b_key2 = b_value2
b_key3 = $r
b_key4 = 127.0.0.1
|
로그인 후 복사
parse_test_conf.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import ConfigParser
cf = ConfigParser.ConfigParser()
#read config
cf.read( "test.conf" )
# return all section
secs = cf.sections()
print 'sections:', secs
opts = cf.options( "sec_a" )
print 'options:', opts
kvs = cf.items( "sec_a" )
print 'sec_a:', kvs
#read by type
str_val = cf.get( "sec_a" , "a_key1" )
int_val = cf.getint( "sec_a" , "a_key2" )
print "value for sec_a's a_key1:" , str_val
print "value for sec_a's a_key2:" , int_val
#write config
#update value
cf.set( "sec_b" , "b_key3" , "new-$r" )
#set a new value
cf.set( "sec_b" , "b_newkey" , "new-value" )
#create a new section
cf.add_section('a_new_section')
cf.set('a_new_section', 'new_key', 'new_value')
#write back to configure file
cf.write(open( "test.conf" , "w" ))
|
로그인 후 복사
터미널 출력 가져오기:
1 2 3 4 5 | sections: ['sec_b', 'sec_a']
options: ['a_key1', 'a_key2']
sec_a: [('a_key1', "i'm value" ), ('a_key2', '22')]
value for sec_a's a_key1: i'm value
value for sec_a's a_key2: 22
|
로그인 후 복사
업데이트된 테스트.
1 2 3 4 5 6 7 8 9 10 11 12 13 | [sec_b]
b_newkey = new -value
b_key4 = 127.0.0.1
b_key1 = 121
b_key2 = b_value2
b_key3 = new - $r
[sec_a]
a_key1 = i'm value
a_key2 = 22
[a_new_section]
new_key = new_value
|
로그인 후 복사
4. Python의 ConfigParser 모듈은 INI 파일에서 작동하는 3개의 클래스를 정의합니다. 이들은 각각 RawConfigParser, ConfigParser 및 SafeConfigParser입니다. RawCnfigParser는 가장 기본적인 INI 파일 읽기 클래스입니다. ConfigParser 및 SafeConfigParser는 %(value)s 변수의 구문 분석을 지원합니다.
test2.conf 구성 파일 설정
1 2 3 4 | [portal]
url = http:
host = localhost
port = 8080
|
로그인 후 복사
RawConfigParser 사용:
1 2 3 4 5 6 7 8 9 10 11 | import ConfigParser
cf = ConfigParser.RawConfigParser()
print "use RawConfigParser() read"
cf.read( "test2.conf" )
print cf.get( "portal" , "url" )
print "use RawConfigParser() write"
cf.set( "portal" , "url2" , "%(host)s:%(port)s" )
print cf.get( "portal" , "url2" )
|
로그인 후 복사
터미널 출력 가져오기:
1 2 3 4 | use RawConfigParser() read
http:
use RawConfigParser() write
%(host)s:%(port)s
|
로그인 후 복사
대신 ConfigParser 사용:
1 2 3 4 5 6 7 8 9 10 11 | import ConfigParser
cf = ConfigParser.ConfigParser()
print "use ConfigParser() read"
cf.read( "test2.conf" )
print cf.get( "portal" , "url" )
print "use ConfigParser() write"
cf.set( "portal" , "url2" , "%(host)s:%(port)s" )
print cf.get( "portal" , "url2" )
|
로그인 후 복사
터미널 출력 가져오기:
1 2 3 4 | use ConfigParser() read
http:
use ConfigParser() write
localhost:8080
|
로그인 후 복사
대신 SafeConfigParser 사용:
1 2 3 4 5 6 7 8 9 10 11 | import ConfigParser
cf = ConfigParser.SafeConfigParser()
print "use SafeConfigParser() read"
cf.read( "test2.conf" )
print cf.get( "portal" , "url" )
print "use SateConfigParser() write"
cf.set( "portal" , "url2" , "%(host)s:%(port)s" )
print cf.get( "portal" , "url2" )
|
로그인 후 복사
터미널 출력 가져오기(효과는 ConfigParser와 동일):
1 2 3 4 | use SafeConfigParser() read
http:
use SateConfigParser() write
localhost:8080
|
로그인 후 복사