Mit Python 2.7.x und ConfigParser, wie man eine Konfigurationsdatei wie folgt erzeugt?Python 2.7 ConfigParser ohne Trennzeichen
[Section]
key1
key2
key3
Mit Python 2.7.x und ConfigParser, wie man eine Konfigurationsdatei wie folgt erzeugt?Python 2.7 ConfigParser ohne Trennzeichen
[Section]
key1
key2
key3
Siehe die Python-Dokumentation. 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)
=
ist Teil des Formats, so dass Sie nicht ConfigParser es wegzulassen sagen kann.
Aber Sie können den Schreiber täuschen, indem Sie ein io.BytesIO()
Objekt übergeben und die Leerzeichen + Gleichheitszeichen entfernen, wenn Sie in eine echte Datei schreiben.
Autarke Beispiel:
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")
Es erzeugt die folgende Ausgabe '[Abschnitt] key1 = key2 = key3 = ' Gibt es trotzdem ** = ** davon abzustreifen – devel0pp3r