Simple ini Config parser examples needed

Glenn Linderman v+python at g.nevcal.com
Tue Dec 2 23:51:17 CET 2008


On approximately 12/2/2008 1:31 PM, came the following characters from 
the keyboard of Chris Rebert:
> On Tue, Dec 2, 2008 at 1:18 PM, RON BRENNAN <rbrennan55 at hotmail.com> wrote:
>   
>> Hello,
>>
>> I have a very simple ini file that I needs parsed.  What is the best way I
>> can parse an ini file that doesn't include sections?
>>
>> As in:
>>
>>     
> Since it appears that ConfigParser requires at least one section
> header, I'll assume the file starts with the following line:
>
> [main]
>   
>> person=tall
>> height=small
>> shoes=big
>>
>>
>> Thats it.  Can anyone help me?
>>     
>
> Completely untested:
>
> import ConfigParser
> config = ConfigParser.RawConfigParser()
> config.readfp(open("path/to/file.cfg"))
> config.get("main", "height") #==> "small"
>
> Cheers,
> Chris
>   

Of course the OP question was that the line you assume isn't there.  But 
if the ini is simple, maybe it is short enough to read into a string, 
then prepend the line, then parse with ConfigParser.

-- 
Glenn -- http://nevcal.com/
===========================
A protocol is complete when there is nothing left to remove.
-- Stuart Cheshire, Apple Computer, regarding Zero Configuration Networking




More information about the Python-list mailing list