Probles parsing a text file

G

greg

heres my text file(well the first two lines of it)

#########################################
# Default BMUS server CFG file #

this file is written with the following code

fi = open('bmus.cfg', 'w')
print 'file opened'
cfg = []
cfg.append('#########################################\n')
cfg.append('# Default BMUS server CFG file
#\n')
fi.writelines(cfg)


i heres the script that im using to parse the file

fi = open('bmus.cfg', 'r')

if fi:
print('Reading Server CFG file')
ll = fi.readlines()
for line in ll:
print(str(line))
if line[:2] == '#' or len(line) == 0:
pass
else:
eval(line)
print("line executed")
else:
print('Could not read Server CFG')

heres the error im getting

D:\DEMOFI~1\blender\pong1>bmus-001-thread-server.py
Reading Server CFG file
#########################################

Traceback (most recent call last):
File "D:\DEMOFI~1\blender\pong1\bmus-001-thread-server.py", line 73, in ?
eval(line)
File "<string>", line 1
#########################################

^
SyntaxError: unexpected EOF while parsing

im using one py script to ouput the txt file using \n as the end of line to
avvoide
any possible windows problems (with it using \r\n).

and then the fiel is erroring here when im reading it back into my program..

many thnas for your help

Greg Brant
 
M

Miki Tebeka

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
 
G

greg

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top