2017-03-06 2 views

답변

0

python 설명서를 참조하십시오. 당신이 그것을 생략 할 configparser을 말할 수 https://docs.python.org/2/library/configparser.html

import ConfigParser 

config = ConfigParser.RawConfigParser() 


config.add_section('Section') 
config.set('Section', 'key1', '') 
config.set('Section', 'key2', '') 
config.set('Section', 'key3', '') 

# Writing our configuration file to 'example.cfg' 
with open('example.cfg', 'wb') as configfile: 
    config.write(configfile) 
+0

그것은 다음 출력 '[제] = KEY1 키 2 = KEY3을 생성 = '은 ** ** = 그로부터 제거 거기다 – devel0pp3r

0

=는 형식의 일부입니다.

그러나 실제 파일에 쓸 때 io.BytesIO() 개체를 전달하고 공백 + 등호를 제거하여 작성자를 속일 수 있습니다.

자 급식 예 :

import ConfigParser,io 

config = ConfigParser.RawConfigParser() 

config.add_section('Section') 
for i in range(1,4): 
    config.set('Section', 'key{}'.format(i), '') 

# Writing our configuration file to 'example.cfg' 
b = io.BytesIO() 
config.write(b) # write in a fake file 

# now write the modified contents to a real file 
with open('output.ini', 'w') as f: 
    for l in b.getvalue().splitlines(): 
     f.write(l.rstrip("= ")+"\n")