newbe's re question

Frederic Rentsch anthra.norell at vtxmail.ch
Thu Sep 21 16:50:08 EDT 2006


Eric_Dexter at msn.com wrote:
> Frederic Rentsch wrote:
>   
>> Eric_Dexter at msn.com wrote:
>>     
>>> Frederic Rentsch wrote:
>>>
>>>       
>>>> 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
>>>>
>>>>         
>>> I want to pass the file name to the subroutine and return a comment
>>> string if it is there maybe it should be simplier.  I probily should
>>> have the option of grabbing the comment in other related routines.  I
>>> am pretty ambitious with the main program.  I did notice some code in
>>> tcl that would be usefull to the app If I compile it..  I am probily
>>> not ready for that though..
>>>
>>> http://www.dexrow.com
>>>
>>>
>>>       
>> Eric,
>>      I'm beginning to enjoy this. I'm sure we'll sort this out in no
>> time if we proceed methodically. Imagine you are a teacher and I am your
>> student. This is a quiz. I have to take it and you need to explain to me
>> the problem you want me to solve. If you don't explain it clearly, I
>> will not know what I have to do and cannot do the quiz. If you answer my
>> questions above, your description of the problem will be clear and I can
>> take the quiz. Okay?
>>
>> Frederic
>>     
>
>
> instr   1
>          a1 oscil p4, p5, 1 ; simple oscillator    ; comment is
> sometimes here
>             out a1
>        endin
>
>
> I need to know the file I wan't to grab this from I need to grab this
> out of the larger file and put it into it's own file,  I need to know
> what instr the user wants.  I need to know what file to put it into and
> it would be usefull to have the comment line returned (if any).
>
> I did just get python essential reference 3rd edition..  If there is a
> better reference on just the subject I am after I would be glad to grab
> it when I get cash again.
>
>   

Eric,

   Tell us the story. Are you a student? A musician? What are you doing 
with these files? Where do you get them from? What for?

Frederic




More information about the Python-list mailing list