473,383 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Reading multiline values using ConfigParser

Hi,
I have a configfile, in fact, I am providing a configfile in the
format:

[Information]
Name: Foo
Author: Bar

Testcases:
tct123
tct124
tct101

The last values is a multi-line.

ConfigParser is unable to recognize a multi-line value and splits out
error.

C:\ATF-Tasks>python CreateTask.py
Traceback (most recent call last):
File "CreateTask.py", line 13, in ?
config.read('TaskDetails.txt')
File "C:\Python24\lib\ConfigParser.py", line 267, in read
self._read(fp, filename)
File "C:\Python24\lib\ConfigParser.py", line 490, in _read
raise e
ConfigParser.ParsingError: File contains parsing errors:
TaskDetails.txt
[line 15]: 'tct123\n'
[line 16]: 'tct124\n'
[line 17]: 'tct101\n'

I am using ConfigParser in the following way:

config = ConfigParser.ConfigParser()
config.read('TaskDetails.txt')
config.get("Information","Testcases"):

Is there anyway, I can include multi-line value in the configfile? I
was thinking of following option:value for a portion of the file and
read the portion with multi-line as a normal file, but ConfigParser()
is not allowing multi-line value itself.

Any ideas/ Suggestions? :

Ofcourse, throw away ConfigParser and use your own parser is there,
but I would like to use that as the last option.

Thanks,
Senthil

Jun 20 '07 #1
4 14088
On Jun 20, 10:35 pm, "John Krukoff" <jkruk...@ltgc.comwrote:
Is there anyway, I can include multi-line value in the configfile? I
>
Following the link to RFC 822 (http://www.faqs.org/rfcs/rfc822.html)
indicates that you can spread values out over multiple lines as long as
there is a space or tab character imeediately after the CRLF.
Thanks for the response. It did work!
>>config = ConfigParser()
config.read("Testcases.txt")
['Testcases.txt']
>>output = config.get("Information", "Testcases")
print output
tct123
tct124
tct125
>>output
'\ntct123\ntct124\ntct125'
>>>
However, as I am going to provide Testcases.txt to be "user editable",
I cannot assume or "ask users" to provide value testcases surronded by
spaces. I got to figure out a workaround here.

Thanks,
Senthil

Jun 21 '07 #2
On Thu, 2007-06-21 at 02:51 +0000, Phoe6 wrote:
On Jun 20, 10:35 pm, "John Krukoff" <jkruk...@ltgc.comwrote:
Is there anyway, I can include multi-line value in the configfile? I

Following the link to RFC 822 (http://www.faqs.org/rfcs/rfc822.html)
indicates that you can spread values out over multiple lines as long as
there is a space or tab character imeediately after the CRLF.

Thanks for the response. It did work!
>config = ConfigParser()
config.read("Testcases.txt")
['Testcases.txt']
>output = config.get("Information", "Testcases")
print output

tct123
tct124
tct125
>output
'\ntct123\ntct124\ntct125'
>>

However, as I am going to provide Testcases.txt to be "user editable",
I cannot assume or "ask users" to provide value testcases surronded by
spaces. I got to figure out a workaround here.
Since you don't actually need a multi-line entry but rather a list of
single-line entries, you could ask your users to enter something like
this:

TestcaseCount=3
Testcase1=tct123
Testcase2=tct124
Testcase3=tct125

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net
Jun 21 '07 #3
Phoe6 wrote:
On Jun 20, 10:35 pm, "John Krukoff" <jkruk...@ltgc.comwrote:
>>Is there anyway, I can include multi-line value in the configfile? I
>Following the link to RFC 822 (http://www.faqs.org/rfcs/rfc822.html)
indicates that you can spread values out over multiple lines as long as
there is a space or tab character imeediately after the CRLF.

Thanks for the response. It did work!
>>>config = ConfigParser()
config.read("Testcases.txt")
['Testcases.txt']
>>>output = config.get("Information", "Testcases")
print output

tct123
tct124
tct125
>>>output
'\ntct123\ntct124\ntct125'

However, as I am going to provide Testcases.txt to be "user editable",
I cannot assume or "ask users" to provide value testcases surronded by
spaces. I got to figure out a workaround here.

Thanks,
Senthil
I do this a lot and implement it as:

[Testcases]
case_001=tct123
case_002=tct124
case_003=tct101

Then it is pretty easy to do something in my code like:

section='Testcases'
cases=[x for x in INI.options(section) if x.startswith('case_')]

for case in cases:
casefile=INI.get(section, case)
#
# Process each casefile here
#

-Larry
Jun 21 '07 #4
On Jun 21, 7:34 pm, "John Krukoff" <jkruk...@ltgc.comwrote:
Is there anyway, I can include multi-line value in the configfile? I
Following the link to RFC 822 (http://www.faqs.org/rfcs/rfc822.html)
indicates that you can spread values out over multiple lines as long as
there is a space or tab character imeediately after the CRLF.
Thanks for the response. It did work!
>>config = ConfigParser()
>>config.read("Testcases.txt")
['Testcases.txt']
>>output = config.get("Information", "Testcases")
>>print output
tct123
tct124
tct125
>>output
'\ntct123\ntct124\ntct125'
However, as I am going to provide Testcases.txt to be "user editable",
I cannot assume or "ask users" to provide value testcases surronded by
spaces. I got to figure out a workaround here.

Sounds like you're stuck modifying ConfigParser to do what you want, or
writing your own configuration file parsing utilities.
From looking through the ConfigParser source, looks like all the parsing

work is inside the _read method, so shouldn't be too painful to make a
subclass that does what you want.
I took the approach of Subclassing ConfigParser to support multiline
values without leading white-spaces, but am struct at which position
in _read I should modify to accomodate the non-leading whitespace
based multiline values.

I can guess, this portion in the _read function will require change,
any change to this affects the whole of parsing. :-( Can someone who
has done this before or understands ConfigParser better help me?

# Section I am talking about
if line[0].isspace() and cursect is not None and optname:

value = line.strip()
if value:
cursect[optname] = "%s\n%s" % (cursect[optname], value)
# _read method

def _read(self, fp, fpname):
cursect = None
optname = None
lineno = 0
e = None
while True:
line = fp.readline()
if not line:
break
lineno = lineno + 1
# comment or blank line?
if line.strip() == '' or line[0] in '#;':
continue
if line.split(None, 1)[0].lower() == 'rem' and line[0] in "rR":
# no leading whitespace
continue
# continuation line
print "line:%s\tcursect:%s\toptname:%s"%(line,cursect,op tname)

if line[0].isspace() and cursect is not None and optname:

value = line.strip()
if value:
cursect[optname] = "%s\n%s" % (cursect[optname], value)
# a section header or option header?
else:
# is it a section header?
mo = self.SECTCRE.match(line)
if mo:
sectname = mo.group('header')
if sectname in self._sections:
cursect = self._sections[sectname]
elif sectname == ConfigParser.DEFAULTSECT:
cursect = self._defaults
else:
cursect = {'__name__':sectname}
self._sections[sectname] = cursect
# So sections can't start with a continuation line
optname = None
elif cursect is None:
raise ConfigParser.MissingSectionHeaderError(fpname, lineno,
line)
# an option line?
else:
mo = self.OPTCRE.match(line)
if mo:
optname, vi, optval = mo.group('option','vi','value')
if vi in ('=',':') and ';' in optval:
# ';' is a comment delimiter only if it follows
# a spacing character
pos = optval.find(';')
if pos != -1 and optval[pos-1].isspace():
optval = optval[:pos]
optval = optval.strip()
# allow empty values
if optval == '""':
optval = ''
optname = self.optionxform(optname.rstrip())
cursect[optname] = optval
else:
if not e:
e = ConfigParser.ParsingError(fpname)
e.append(lineno, repr(line))
if e:
raise e

--
Senthil

Jun 25 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thomas Guettler | last post by:
Hi, I need a config like this: ignore=".*/foodir/.*\.pyc" ignore=".*/foodir/.*~" .... The ConfigParser of the standard library can't handle this,
6
by: Suresh Kumaran | last post by:
Hi All, Does anybody know the sytax in VB.NET to write the contents of a multiline text box to a text file? Appreciate help. Suresh
1
by: s99999999s2003 | last post by:
hi i used ConfigParser to read a config file. I need the config file to have identical sections. ie : blah = "some server" blah = "some destination" end= ''
1
by: pipehappy | last post by:
Hello everyone: I came across the module ConfigParser and can use it correctly. import ConfigParser fp = open('test.cfg','w+') config = ConfigParser.ConfigParser() config.readfp(fp)...
0
by: Mike Collins | last post by:
I someone can please help, I am about at an end in trying to figure this out. I am adding some dynamic controls to my page (I found out that I was supposed to be doing that in the oninit event,...
0
by: Phoe6 | last post by:
Hi All, I am able to use urlib2 through proxy. I give proxy credentials and use # Set the Proxy Address proxy_ip = "10.0.1.1:80" proxy_user = 'senthil_or' proxy_password_orig='password'
5
by: Kevin Walzer | last post by:
I want to write some variables (user preferences, specifically) to a text file and then read the values from that file. Here is my code to write the data: verbosemodes= """ Detailed = "-vv"...
3
by: Phoe6 | last post by:
Hi, Am starting a new thread as I fear the old thread which more than a week old can go unnoticed. Sorry for the multiple mails. I took the approach of Subclassing ConfigParser to support...
4
by: Hamish Moffatt | last post by:
SafeConfigParser is supposed to be safer than ConfigParser, but calling set with a string value containing '%' generates exceptions when you get() it back. Python 2.5.1 (r251:54863, Apr 25 2007,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.