En utilisant Python 2.7.x et ConfigParser, comment générer un fichier de configuration comme suit?Python 2.7 ConfigParser sans delimiter
[Section]
key1
key2
key3
En utilisant Python 2.7.x et ConfigParser, comment générer un fichier de configuration comme suit?Python 2.7 ConfigParser sans delimiter
[Section]
key1
key2
key3
Reportez-vous à la documentation 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)
=
fait partie du format de sorte que vous ne pouvez pas dire ConfigParser de l'omettre.
Mais vous pouvez tromper l'écrivain en passant un objet io.BytesIO()
et enlevez les espaces + signe égal lors de l'écriture dans un fichier réel.
autonome exemple:
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")
Il produit la sortie suivante '[section] clé1 = key2 = key3 = ' Y at-il de toute façon à bande ** = ** de celui-ci – devel0pp3r