configparser

Stop ConfigParser adding spaces to delims after upgrade from python 2.7.3 to 2.7.9

巧了我就是萌 提交于 2019-12-01 11:47:52
问题 After being forced to use a later version of python, ConfigParser now insists on adding spaces to each side of any delims when modifying a configuration file. e.g. setting=90 becomes: setting = 90 This was not the behavior in the earlier version, and I cannot find a way of controlling this behavior, can anyone help? My test code looks like this: import ConfigParser import os config = ConfigParser.ConfigParser() cfgfile = '/home/osmc/bin/test/config.txt' os.system('sudo echo "[section]" > ' +

Keep ConfigParser output files sorted

╄→гoц情女王★ 提交于 2019-11-30 20:19:47
I've noticed with my source control that the content of the output files generated with ConfigParser is never in the same order. Sometimes sections will change place or options inside sections even without any modifications to the values. Is there a way to keep things sorted in the configuration file so that I don't have to commit trivial changes every time I launch my application? Alexander Ljungberg Looks like this was fixed in Python 3.1 and 2.7 with the introduction of ordered dictionaries: The standard library now supports use of ordered dictionaries in several modules. The configparser

Configparser set with no section

╄→尐↘猪︶ㄣ 提交于 2019-11-30 18:52:19
问题 Is there a way for configparser in python to set a value without having sections in the config file? If not please tell me of any alternatives. Thank you. more info: So basically I have a config file with format: Name: value It's a system file that I want to change the value for a given name. I was wondering if this can be done easily with a module instead of manually writing a parser. 回答1: You could use the csv module to do most of work of parsing the file and writing it back out after you

How to use variables already defined in ConfigParser

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 17:41:10
I'm using ConfigParser in Python config.ini is [general] name: my_name base_dir: /home/myhome/exp exe_dir: ${base_dir}/bin Here I want exp_dir becomes /home/myhome/exp/bin not ${base_dir}/bin . It means ${base_dir} would be substituted to /home/myhome/exp automatically . You can use ConfigParser interpolation On top of the core functionality, SafeConfigParser supports interpolation. This means values can contain format strings which refer to other values in the same section, or values in a special DEFAULT section. Additional defaults can be provided on initialization. For example: [My Section]

Parsing configure file with same section name in python

喜你入骨 提交于 2019-11-30 13:19:52
问题 I try to parse file like: [account] User = first [account] User = second I use ConfigParser in Python, but when i read file: Config = configparser.ConfigParser() Config.read(file) print (Config.sections()) I have error: While reading from ... : section 'account' already exists How can i parse this file? Are any another library? (prefer for python3) 回答1: If what you want is to simply merge identically named sections (latest one wins), simply pass the strict=False option to the constructor

ConfigParser and String interpolation with env variable

混江龙づ霸主 提交于 2019-11-30 12:25:33
问题 it's a little bit I'm out of python syntax and I have a problem in reading a .ini file with interpolated values. this is my ini file: [DEFAULT] home=$HOME test_home=$home [test] test_1=$test_home/foo.csv test_2=$test_home/bar.csv Those lines from ConfigParser import SafeConfigParser parser = SafeConfigParser() parser.read('config.ini') print parser.get('test', 'test_1') does output $test_home/foo.csv while I'm expecting /Users/nkint/foo.csv EDIT: I supposed that the $ syntax was implicitly

How to access the variables declared inside functions in python

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 10:00:30
问题 I have the following code that reads the configuration file and stores the results in some variables as a list import ConfigParser def read_config_file(): config = ConfigParser.ConfigParser() cnf_path = 'config_files/php.sr' config.read(cnf_path) if config.has_section('basic'): if config.has_option('basic', 'basic'): php_bsc_mdls = config.get('basic', 'basic').split(',') if config.has_section('advance'): if config.has_option('advance','advance'): php_adv_mdls = config.get('advance', 'advance'

Parsing configure file with same section name in python

我的梦境 提交于 2019-11-30 09:11:50
I try to parse file like: [account] User = first [account] User = second I use ConfigParser in Python, but when i read file: Config = configparser.ConfigParser() Config.read(file) print (Config.sections()) I have error: While reading from ... : section 'account' already exists How can i parse this file? Are any another library? (prefer for python3) If what you want is to simply merge identically named sections (latest one wins), simply pass the strict=False option to the constructor (added in Python 3.2). You effectively get dict.update() behavior as the duplicate sections are merged in.

ConfigParser with Unicode items

半腔热情 提交于 2019-11-30 08:22:19
my troubles with ConfigParser continue. It seems it doesn't support Unicode very well. The config file is indeed saved as UTF-8, but when ConfigParser reads it it seems to be encoded into something else. I assumed it was latin-1 and I thougt overriding optionxform could help: -- configfile.cfg -- [rules] Häjsan = 3 ☃ = my snowman -- myapp.py -- # -*- coding: utf-8 -*- import ConfigParser def _optionxform(s): try: newstr = s.decode('latin-1') newstr = newstr.encode('utf-8') return newstr except Exception, e: print e cfg = ConfigParser.ConfigParser() cfg.optionxform = _optionxform cfg.read(

Writing comments to files with ConfigParser

大城市里の小女人 提交于 2019-11-30 08:07:44
How can one write comments to a given file within sections? If I have: import ConfigParser with open('./config.ini', 'w') as f: conf = ConfigParser.ConfigParser() conf.set('DEFAULT', 'test', 1) conf.write(f) I will get the file: [DEFAULT] test = 1 But how can I get a file with comments inside [DEFAULT] section, like: [DEFAULT] ; test comment test = 1 I know I can write codes to files by doing: import ConfigParser with open('./config.ini', 'w') as f: conf = ConfigParser.ConfigParser() conf.set('DEFAULT', 'test', 1) conf.write(f) f.write('; test comment') # but this gets printed after the