2

I'm having trouble using a config file, because the option starts with #, thus python treats it as a comment (like it should).

The part of the config file that is not working:

[channels]
#channel

As you may see, it's an IRC channel, that is why it needs the #. Now I could use some ugly method of adding the # everytime I need it, but I'd prefer to keep it clean.

So is there any way to ignore this? So that when I were to print the option, it would start with

3
  • Are you using a known configuration format, like INI? Commented Feb 27, 2013 at 22:38
  • 1
    Your config file is either valid Python code or it isn't. In the former case it needs to be formatted like any other Python code, and in the latter case you should definitely not just be feeding it into the Python interpreter. What's the context in which you're using this file?
    – bdesham
    Commented Feb 27, 2013 at 22:39
  • Waleed, I am using an .ini file. Commented Feb 27, 2013 at 22:42

4 Answers 4

2

If your setting that in a python file you can escape the # with \

Otherwise I think that should be in a config file with other syntax that doesn't treat # as a commented line

1
  • I am using an INI file, and I am calling it with a SafeConfigParser() function. Does the file type matter when I am just calling it with that, because afaik it just loads it as raw text. Commented Feb 27, 2013 at 22:46
1

You are probably using ConfigParser - which you should mention btw - then you have to pre-/postprocess the configfile before feeding it to the parser, because ConfigParser ignores the comment-parts.

I can think of two ways, both of them make use of the readfp, instead of the read-method of the ConfigParser-class: 1) subclass StreamWriter and StreamReader from the codecs-module and use them to wrap the opening-process in a transparent recoding. 2) use StringIO from the io module like:

from io import StringIO
...
s = configfile.read()
s.replace("#","_")
f = StringIO(unicode(s))
configparser.readfp(f)

And if you don't have to use an "ini"-file syntax take a look at the json module. I use it more often then the ini-file for configuration, especially if the config-files shouldn't be manually edited by simple users.

my_config={
  "channels":["#mychannel", "#yourchannel"],
  "user"="bob",
  "buddy-list":["alice","eve"],
  }

import json
with open(configfile, 'rw') as cfg:
  cfg.write(json.dumps(my_config))
5
  • How would I go about doing that? I am pretty (very) new to all this. I just read the configparser docs, and I don't know what else to use to use my config file. Commented Feb 27, 2013 at 22:50
  • That would still give me the value without the #, correct? So I might as well just remove #'s from the config file, and add them during interpretation? Commented Feb 27, 2013 at 23:11
  • indeed. it would replace the '#' temporarily with another unlikely used, but valid char. i took the underline. but you could circumvent the whole issue by using the json-module. It would further have the advantage of allowing nesting and you could do a transparent conversion from dict to json and vice versa. Commented Feb 27, 2013 at 23:14
  • I am going to avoid this by setting the channels as values instead of options, so that the # isn't at the beginning of the line. That should fix it without having to implement a completely new module (as I said, I'm quite the beginner :p ). Thanks for your help, I will look into json when I decide to rewrite this whole thing again. Commented Feb 27, 2013 at 23:16
  • you need exactly two function from the json-module: loads and dumps - it's especially for beginners the most pain-free way imho. Commented Feb 27, 2013 at 23:24
0

ConfigParser has no way to not ignore lines beginning with '#'.

ConfigParser.py, line 476:

        # comment or blank line?
        if line.strip() == '' or line[0] in '#;':
            continue

No way to turn it off.

0

In your defense ConfigParser is letting you make this mistake:

import sys
import ConfigParser

config = ConfigParser.RawConfigParser()
config.add_section('channels')
config.set('channels', '#channel', 'true')

config.write(sys.stdout)

Produces this output:

[channels]
#channel = true

However you can give section names that start with a # like so:

import sys
import ConfigParser

config = ConfigParser.RawConfigParser()
config.add_section('#channels')
config.set('#channels', 'channel', 'true')

config.write(sys.stdout)

with open('q15123871.cfg', 'wb') as configfile:
    config.write(configfile)

config = ConfigParser.RawConfigParser()
config.read('q15123871.cfg')
print config.get('#channels', 'channel')

Which produces the output:

[#channels]
channel = true

true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.