E
Eric_Dexter
I was trying to add this to my project but I must be missing some
includes or there is a serius error somewhere
I cut and pasted this.. It seems to be crashing my program.. I am not
sure that I have all the right imports.. seems to be fine when I go to
an older version of the file... I uploaded it onto source forge.
https://sourceforge.net/project/showfiles.php?group_id=156455&package...
http://www.dexrow.com
includes or there is a serius error somewhere
Anthra said:Dexter,
Here's a function that screens out all instrument blocks and puts them into a dictionary keyed on the instrument number:
--------------------------------------------
def get_instruments (file_name):
INSIDE = 1
OUTSIDE = 0
f = file (file_name, 'ra')
state = OUTSIDE
instruments = {}
instrument_segment = ''
for line in f:
if state == OUTSIDE:
if line.startswith ('<CsInstruments'):
state = INSIDE
instrument_segment += line
else:
instrument_segment += line
if line.lstrip ().startswith ('instr'):
instrument_number = line.split () [1]
elif line.startswith ('</CsInstruments'):
instruments [instrument_number] = instrument_segment
instrument_segment = ''
state = OUTSIDE
f.close ()
return instruments
------------------------------------------------
You have received good advice on using parsers: "beautiful soup" or "pyparse". These are powerful tools capable of doing complicated
extractions. Yours is not a complicated extraction. Simon tried it with "beautiful soup". That seems simple enough, though he finds
the data by index leaving open where he gets the index from. There's surely a way to get the data by name.
Contrary to the parser the function will miss if tags take liberties with upper-lower case letters as they are probably
allowed by the specification. A regular expression might have to be used, if they do.
From your description I haven't been able to infer what the final format of your data is supposed to be. So I cannot tell you
how to go on from here. You'll find out. If not, just keep asking.
The SE solution which you said couldn't work out would be the following. It makes the same dictionary the function makes and it is
case-insensitive:
------------------------------------------------
if segment:
instr_line = Instrument_Number (segment)
instrument_number = instr_line.split ()[1]
instruments [instrument_number] = segment
--------------------------------------------------
(If you're on Windows and the CRs bother you, take them out with an additional definition when you make your
Instrument_Block_Filter: (13)= or "\r=")
Regards
Frederic
----- Original Message -----
From: <[email protected]>
Newsgroups: comp.lang.python
To: <[email protected]>
Sent: Wednesday, August 30, 2006 1:51 AM
Subject: Re: newbe question about removing items from one file to another file
...
etc.
I cut and pasted this.. It seems to be crashing my program.. I am not
sure that I have all the right imports.. seems to be fine when I go to
an older version of the file... I uploaded it onto source forge.
https://sourceforge.net/project/showfiles.php?group_id=156455&package...
http://www.dexrow.com