Probles parsing a text file

greg zorlord at zorlord.karoo.co.uk
Tue Jan 6 04:36:47 EST 2004


thanks Miki.

what would you suggest
somthing like
if line[:8] == "setVar1":
    #do somthing
elif line[:8] == "setVar2":
    #do somthing else
but this would be extreamly slow if there server.cfg has many directives in
it
then when the last directive is found (eg: setVar117) and this is the 116th
elif
then it will have to check through
if line[:8] == "setVar1":
    #do somthing
elif line[:8] == "setVar2":
    #do somthing else
elif line[:8] == "setVar3":
    #do somthing
elif line[:8] == "setVar4":
    #do somthing else
elif line[:8] == "setVar5":
    #do somthing
...............
..........
.....
elif line[:8] == "setVar117":
    #execute code for 117

what do you think

"Miki Tebeka" <miki.tebeka at zoran.com> wrote in message
news:4f0a9fdb.0401052354.549cece4 at posting.google.com...
> Hello,
>
> > heres my text file(well the first two lines of it)
> >
> > #########################################
> > #            Default BMUS server CFG file                   #
> ...
> > i heres the script that im using to parse the file
> ...
> Try:
> #!/usr/bin/env python
> try:
>     fo = open("server.cfg")
>     print "Reading server CFG file"
>     for line in fo:
>         line = line.strip() # Strip white spaces
>         if (not line) or line.startswith("#"):
>             continue
>         eval(line) # *** UNSAFE ***
>         print "line executed"
> except IOError, e:
>     print "Can't read server CFG (%s)" % e
>
> Note that using "eval" is very unsafe. Someone might place
> 'shutil.rmtree("/")' inside the CFG file.
>
> > many thnas for your help
> No problem.
>
> Miki





More information about the Python-list mailing list