python - Configparser set with no section -


is there way configparser in python set value without having sections in config file?

if not please tell me of alternatives.

thank you.

more info: have config file format: name: value it's system file want change value given name. wondering if can done module instead of manually writing parser.

you use csv module of work of parsing file , writing out after made changes -- should relatively easy use. got idea 1 of answers similar question titled using python's configparser read file without section name.

however made number of changes, including coding work in both python 2 & 3, unhardcoding key/value delimiter (but colons default), along several optimizations.

from __future__ import print_function # module main() test function import csv import sys py3 = sys.version_info[0] > 2  def read_properties(filename, delimiter=':'):     ''' reads given properties file each line of format key=value.         returns dictionary containing pairs.             filename -- name of file read     '''     open_kwargs = {'mode': 'r', 'newline': ''} if py3 else {'mode': 'rb'}     open(filename, **open_kwargs) csvfile:         reader = csv.reader(csvfile, delimiter=delimiter, escapechar='\\',                             quoting=csv.quote_none)         return {row[0]: row[1] row in reader}  def write_properties(filename, dictionary, delimiter=':'):     ''' writes provided dictionary in key sorted order properties         file each line in format: key<delimiter>value             filename -- name of file written             dictionary -- dictionary containing key/value pairs.     '''     open_kwargs = {'mode': 'w', 'newline': ''} if py3 else {'mode': 'wb'}     open(filename, **open_kwargs) csvfile:         writer = csv.writer(csvfile, delimiter=delimiter, escapechar='\\',                             quoting=csv.quote_none)         writer.writerows(sorted(dictionary.items()))  def main():     data = {         'answer': '6*7=42',         'knights': 'ni!',         'spam': 'eggs',     }      filename='test.properties'     write_properties(filename, data)     newdata = read_properties(filename)      print('read in: ')     print(newdata)     print()      open(filename, 'rb') propfile:         contents = propfile.read()     print('file contents: (%d bytes)' % len(contents))     print(repr(contents))      print(['failure!', 'success!'][data == newdata])  if __name__ == '__main__':      main() 

Comments