configparser

config parser: Choosing name and value delimiter

冷暖自知 提交于 2019-12-10 22:53:40
问题 Let's say I have one test.ini file with the following lines: [A] name1 [0,1]=0 name2 a:b:c / A:B:C [0,1]=1 When I parse it like this: A = ConfigParser.ConfigParser() with codecs.open('test.ini', 'r') as f: A.optionxform = str A.readfp(f) for section_name in A.sections(): print 'Section:', section_name print 'Options:', A.options(section_name) for name, value in A.items(section_name): print 'name-value pair:' print '%s' % (name) print '%s' % (value) I get the following output: Section: A

configparser not working in Python 3.4, NoSectionError but works fine in PyCharm

寵の児 提交于 2019-12-10 20:02:04
问题 I worked on a Python 3.4 script in PyCharm 4.5. (repo: https://github.com/Djidiouf/bbot ) In it, I used import configparser without any problem for retrieving some values in a config.cfg: config = configparser.RawConfigParser() config.read('config.cfg') server = config.get('bot_configuration', 'server') channel = config.get('bot_configuration', 'channel') botnick = config.get('bot_configuration', 'botnick') port = config.getint('bot_configuration', 'port') Now, I want to deploy it on a Debian

Change value in ini file using ConfigParser Python

坚强是说给别人听的谎言 提交于 2019-12-09 10:10:26
问题 So, I have this settings.ini : [SETTINGS] value = 1 And this python script from ConfigParser import SafeConfigParser parser = SafeConfigParser() parser.read('settings.ini') print parser.get('SETTINGS', 'value') As you can see, I want to read and then replace the value "1" by another one. All I was able to do so far is to read it. I searched on the net how to replace it but I didn't find. 回答1: As from the examples of the documentation: https://docs.python.org/2/library/configparser.html parser

What's better, ConfigObj or ConfigParser?

做~自己de王妃 提交于 2019-12-09 04:18:10
问题 Which is better for creating a settings file for Python programs, the built-in module (ConfigParser), or the independent project (ConfigObj)? 回答1: I recently switched from configparser to configobj, and I'm thrilled to have done so. For me, the big difference is configobj's validator. It lets me very easily/succinctly (1) define the type and acceptable values for each entry, and (2) set defaults. Those two features save me a lot of code and prevent a lot from going wrong. Plus, there's really

Use of external modules in a logging configuration file

大兔子大兔子 提交于 2019-12-07 17:55:21
问题 I have setup the following configuration file for the logging module in python2.7 with logstash [loggers] keys=root,test [handlers] keys=remote [formatters] keys=standard [logger_root] level=NOTSET handlers=remote [logger_test] level=DEBUG handlers=remote propagate=0 qualname=test [handler_remote] class=logstash.LogstashHandler level=NOTSET formatter=standard args=(os.environ['hostname'], int(os.environ['port'])) [formatter_standard] format=%(levelname)s - %(message)s datefmt= class=logging

Handling duplicate keys with ConfigParser [duplicate]

跟風遠走 提交于 2019-12-07 12:58:03
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Python Config Parser (Duplicate Key Support) I'm trying to read an INI format project file in Python. The file contains duplicate keys (having unique values) within a section. For example, one of the sections looks like this: [Source Files] Source="file1.c" Source="file2.c" Source="file3.c" If I read this using the following code config = configparser.ConfigParser( strict=False ) config.read( "project/file/name"

Adding comment with configparser

大城市里の小女人 提交于 2019-12-07 11:19:27
问题 I can use the ConfigParser module in python to create ini-files using the methods add_section and set (see sample in http://docs.python.org/library/configparser.html). But I don't see anything about adding comments. Is that possible? I know about using # and ; but how to get the ConfigParser object to add that for me? I don't see anything about this in the docs for configparser. 回答1: If you want to get rid of the trailing = , you can subclass ConfigParser.ConfigParser as suggested by

How to read config(.ini) file in python which will work on 2.7 and 3.x python

陌路散爱 提交于 2019-12-06 07:36:43
问题 Should I use ConfigParser which is compatible with python 2.7 and 3.x or do you suggest any other module in python which is compatible with both versions of python for reading config file? 回答1: You can make use of configparser backport, so it will work on both Python version. pip install configparser 回答2: Contrary to the other answers here, you don't need to install any extra packages to write INI-parsing code that is compatible with Python 2 and 3. Python 3.0's configparser is just a renamed

Use of external modules in a logging configuration file

99封情书 提交于 2019-12-05 21:40:27
I have setup the following configuration file for the logging module in python2.7 with logstash [loggers] keys=root,test [handlers] keys=remote [formatters] keys=standard [logger_root] level=NOTSET handlers=remote [logger_test] level=DEBUG handlers=remote propagate=0 qualname=test [handler_remote] class=logstash.LogstashHandler level=NOTSET formatter=standard args=(os.environ['hostname'], int(os.environ['port'])) [formatter_standard] format=%(levelname)s - %(message)s datefmt= class=logging.Formatter Unfortunately, this is about as short as I can make the file for this example. I use a module

Handling duplicate keys with ConfigParser [duplicate]

蹲街弑〆低调 提交于 2019-12-05 20:25:41
This question already has answers here : Closed 6 years ago . Possible Duplicate: Python Config Parser (Duplicate Key Support) I'm trying to read an INI format project file in Python. The file contains duplicate keys (having unique values) within a section. For example, one of the sections looks like this: [Source Files] Source="file1.c" Source="file2.c" Source="file3.c" If I read this using the following code config = configparser.ConfigParser( strict=False ) config.read( "project/file/name" ) print( config.get( "Source Files", "Source" ) ) the result is "file3.c" Is there any way to get a