newbe's re question

Frederic Rentsch anthra.norell at vtxmail.ch
Wed Sep 20 12:48:01 CEST 2006


Eric_Dexter at msn.com wrote:
> All I am after realy is to change this
>
>  reline = re.line.split('instr', '/d$')
>
> into something that grabs any line with instr in it take all the
> numbers and then grab any comment that may or may not be at the end of
> the line starting with ; until the end of the line including white
> spaces..  this is a corrected version from
>
> http://python-forum.org/py/viewtopic.php?t=1703
>
> thanks in advance the hole routine is down below..
>
>
>
>
>
>
> [code]
> def extractCsdInstrument (input_File_Name, output_File_Name,
> instr_number):
>
>     "takes an .csd input file and grabs instr_number instrument and
> creates output_File_Name"
>     f = open (input_File_Name , 'r')                #opens file passed
> in to read
>     f2 = open (output_File_Name, 'w')               #opens file passed
> in to write
>     instr_yes = 'false'                             #set flag to false
>
>     for line in f:                                  #for through all
> the lines
>       if "instr" in line:                           #look for instr in
> the file
>            if instr_yes == 'true':                    #check to see if
> this ends the instr block
>                break                                #exit the block
>
>            reline = re.line.split('instr', '/d$')     #error probily
> split instr and /d (decimal number into parts) $ for end of line
>            number = int(reline[1])                  #convert to a
> number maybe not important
>                 if number == instr_number:            #check to see if
> it is the instr passed to function
>                 instr_yes = "true":                 #change flag to
> true because this is the instr we want
>       if instr_yes = "true":                        #start of code to
> copy to another file
>            f2.write(f.line)                         #write line to
> output file
>
>     f.close                                         #close input file
>     f2.close          
>
> [/code]
>
>   
Eric,
      From your problem description and your code it is unclear what 
exactly it is you want. The task appears to be rather simple, though, 
and if you don't get much useful help I'd say it is because you don't 
explain it very well.
      I believe we've been through this before and your input data is 
like this

   data = '''
   <CsoundSynthesizer>;
     ; test.csd - a Csound structured data file
 
   <CsOptions>
     -W -d -o tone.wav
   </CsOptions>
 
   <CsVersion>    ;optional section
     Before 4.10  ;these two statements check for
     After 4.08   ;   Csound version 4.09
   </CsVersion>
 
   <CsInstruments>
     ; originally tone.orc
     sr = 44100
     kr = 4410
     ksmps = 10
     nchnls = 1
     instr   1
         a1 oscil p4, p5, 1 ; simple oscillator
            out a1
       endin
   </CsInstruments>

   <CsScore>
     ; originally tone.sco
     f1 0 8192 10 1
     i1 0 1 20000 1000 ;play one second of one kHz tone
     e
   </CsScore>

   </CsoundSynthesizer>

Question 1: Is this your input?
if yes:
    Question 1.1: What do you want to extract from it? In what format?
if no:
    Question 1.1: What is your input?
    Question 1.2: What do you want to extract from it? In what format?
Question 2: Do you need to generate output file names from the data? 
(One file per instrument?)
if yes:
   Question 2.1: What do you want to make your file name from? 
(Instrument number?)


Regards

Frederic




More information about the Python-list mailing list