JP29
JP29

Reputation: 625

New lines with ConfigParser?

I have a config file using configParser:

<br>
[ section one ]<br>
one = Y,Z,X <br><br>
[EG 2]<br>
ias = X,Y,Z<br>

My program works fine reading and processing these values.

However some of the sections are going to be quite large. I need a config file that will allow the values to be on a new line, like this:

[EG SECTION]<br>
EG=<br>
item 1 <br>
item 2 <br>
item 3<br>
etc...

In my code I have a simple function that takes a delimiter (or separator) of the values using string.split() obviously now set to comma. I have tried the escape string of \n which does not work.

Does anyone know if this is possible with python's config parser?
http://docs.python.org/library/configparser.html

# We need to extract data from the config 
def getFromConfig(currentTeam, section, value, delimeter):
    cp = ConfigParser.ConfigParser()
    fileName = getFileName(currentTeam)
    cp.read(fileName)
    try:
        returnedString = cp.get(section, value)
    except: # The config file could be corrupted
        print( "Error reading " + fileName + " configuration file." )
        sys.exit(1) #Stop us from crashing later
    if delimeter != "": # We may not need to split
        returnedList = returnedString.split(delimeter)
    return returnedList

I would use for this:

taskStrings = list(getFromConfig(teamName, "Y","Z",","))

Upvotes: 13

Views: 16890

Answers (3)

shenez Chen
shenez Chen

Reputation: 41

https://docs.python.org/3/library/configparser.html#supported-ini-file-structure

Values can also span multiple lines, as long as they are indented deeper than the first line of the value. Depending on the parser’s mode, blank lines may be treated as parts of multiline values or ignored.

[Multiline Values]
chorus: I'm a lumberjack, and I'm okay
    I sleep all night and I work all day
[Section]
key = multiline
  value with a gotcha

 this = is still a part of the multiline value of 'key'

for your case

[EG SECTION]<br>
EG=<br>
item1<br>
item2<br>
item3<br>

eg = cp.get('EG SECTION', 'EG')
print(eg)

would get some error :Source contains parsing errors: This is "no value" error.

item 1 would be considered as new option with no value item 2,3 are the same, while the EG=
get blank value

if you modify them with "allow_no_value=True"

[EG SECTION]<br>
EG=<br>
item1<br>
item2<br>
item3<br>

cp = ConfigParser.ConfigParser(allow_no_value=True)
eg = cp.get('EG SECTION', 'EG')
print(eg)

you would get blank screen for output, because EG=

Finally,add some indents and split function

[EG SECTION]<br>
EG=<br>
 item1<br>
 item2<br>
 item3<br>

cp = ConfigParser.ConfigParser()
eg = cp.get('EG SECTION', 'EG').split('\n')
print(eg)

you would get ['item1', 'item2', 'item3']

Upvotes: 0

martineau
martineau

Reputation: 123473

The ConfigParser _read() method's docstring says:

Continuations are represented by an embedded newline then leading whitespace.

Or alternatively (as the version in Python 3 puts it):

Values can span multiple lines, as long as they are indented deeper than the first line of the value.

This feature provides a means to split values up and "continue" them across multiple lines. For example, say you had a config file named 'test.ini' which contained:

[EG SECTION]<br>
EG=<br>
  item 1<br>
  item 2<br>
  item 3<br>

You could read the value of EG in the EG SECTION into a list with code like this:

try:
    import ConfigParser as configparser
except ImportError:  # Python 3
    import configparser

cp = configparser.ConfigParser()
cp.read('test.ini')

eg = cp.get('EG SECTION', 'EG')
print(repr(eg))  # -> '\nitem 1\nitem 2\nitem 3'

cleaned = [item for item in eg.strip().split('\n')]
print(cleaned)  # -> ['item 1', 'item 2', 'item 3']

Upvotes: 17

Andre Blum
Andre Blum

Reputation: 391

It seems possible. In my own config file, for example, I have a list object with tuples:

[root]
path: /
redirectlist: [ ( r'^magic', '/file' ),
    ( r'^avplay', '/file' ),
    ( r'^IPTV', '/file' ),
    ( r'^box', '/file' ),
    ( r'^QAM', '/qam' ),
    ( r'.*opentv.*', '/qam' ),
    ( r'.+', '/file' ) ]

and I do:

redirectstr = _configdict.get('root', 'redirectlist')
redirects = eval(redirectstr)

note that I am actually eval'ing that line, which may cause security breaches if used in the wild.

Upvotes: 2

Related Questions