Python 2.7.xとConfigParserを使用して、次のように設定ファイルを生成するにはどうすればよいですか?デリミタなしのPython 2.7 ConfigParser
[Section]
key1
key2
key3
Python 2.7.xとConfigParserを使用して、次のように設定ファイルを生成するにはどうすればよいですか?デリミタなしのPython 2.7 ConfigParser
[Section]
key1
key2
key3
pythonのドキュメントを参照してください。 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)
=
あなたはそれを省略するのConfigParserを伝えることはできませんので、フォーマットの一部です。
しかし、実際のファイルに書き込むときに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")
それは '次の出力が生成され、[セクション]ストリップに キー1 = キー2 = KEY3 = 'あり、とにかく** = **それから – devel0pp3r