473,405 Members | 2,379 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,405 software developers and data experts.

Probles parsing a text file

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
Jul 18 '05 #1
2 3348
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
Jul 18 '05 #2
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" <mi*********@zoran.com> wrote in message
news:4f**************************@posting.google.c om...
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

Jul 18 '05 #3

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

Similar topics

4
by: ralphNOSPAM | last post by:
Is there a function or otherwise some way to pull out the target text within an XML tag? For example, in the XML tag below, I want to pull out 'CALIFORNIA'. ...
3
by: Pir8 | last post by:
I have a complex xml file, which contains stories within a magazine. The structure of the xml file is as follows: <?xml version="1.0" encoding="ISO-8859-1" ?> <magazine> <story>...
1
by: Thomas Kowalski | last post by:
Hi, I have to parse a plain, ascii text file (on local HD). Since the file might be many millions lines long I want to improve the efficiency of my parsing process. The resulting data structure...
4
by: Neil.Smith | last post by:
I can't seem to find any references to this, but here goes: In there anyway to parse an html/aspx file within an asp.net application to gather a collection of controls in the file. For instance...
3
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in...
2
by: hzgt9b | last post by:
I've written a simple javascript page that parses an XML file... (Actually I just modified the "Parsing an XML File" sample from http://www.w3schools.com/dom/dom_parser.asp) The page works great...
13
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple...
1
by: martinsson | last post by:
Hi all! I'm pretty mad about this... dont know what is going on. Im parsing XML file that looks like this: <something> __<item att="something">text<item> __<item...
2
by: python | last post by:
I'm parsing a text file for a proprietary product that has the following 2 directives: #include <somefile> #define <name<value> Defined constants are referenced via <#name#syntax. I'm...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.