newbe question about removing items from one file to another file

Eric_Dexter at msn.com Eric_Dexter at msn.com
Sun Sep 3 22:58:39 EDT 2006


Anthra Norell wrote:
> 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:
>
> ------------------------------------------------
>
> >>> Instrument_Segment_Filter = SE.SE ('<EAT> "~(?i)<CsInstruments>(.|\n)*?</CsInstruments>~==\n\n"  ')
> >>> instrument_segments= Instrument_Segment_Filter ('file_name', '')
> >>> print instrument_segments
> (... see all instrument segments ...)
> >>> Instrument_Number = SE.SE ('<EAT> ~instr.*~==\n')
> >>> instruments ={}
> >>> for segment in instrument_segments.split ('\n\n'):
>             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: <Eric_Dexter at msn.com>
> Newsgroups: comp.lang.python
> To: <python-list at python.org>
> Sent: Wednesday, August 30, 2006 1:51 AM
> Subject: Re: newbe question about removing items from one file to another file
>
>
> >
> > Anthra Norell wrote:
> > > Dexter,
> > >
> > > I looked at the format specification. It contains an example:
> > >
> > > -----------------------------------------------
> > >
> > > <CsoundSynthesizer>;
> > >   ; test.csd - a Csound structured data file
> > >
> > > <CsOptions>
> > >   -W -d -o tone.wav
> > > </CsOptions>
> > >
> ...
> 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_id=201306&release_id=444362
http://www.dexrow.com




More information about the Python-list mailing list