newbe's re question

Eric eric.talevich at
Wed Sep 20 09:30:47 CEST 2006

Eric_Dexter at 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.
> [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')
>     f2 = open (output_File_Name, 'w')
>     instr_yes = 'false' # Python has built-in booleans
>                                # Also, use sane naming: isInstr or hasInstr
>     for line in f:
>       if "instr" in line:
>            if instr_yes == 'true': # Use the bool -- `if hasInstr:`
>                break
>            reline = re.line.split('instr', '/d$')     # ???
>            number = int(reline[1])
>                 if number == instr_number:      # Doesn't need to be 2 lines
>                 instr_yes = "true":                 # the `:` is a syntax error
>       if instr_yes = "true":
>            f2.write(f.line) # odd way of doing it...
>     f.close()
>     f2.close()
> [/code]

Did you read the HOWTO on regular expressions yet? It's included with
the standard documentation, but here's a link:

Now, onto the issues:
1) The escape character is \, not /. Use "\d" to match a number.

2) I'm not sure I understand what you're trying to extract with your
regex. As it is, the expression will return a list containing one item,
a string identical to the line being matched up to, but not including,
the first number. The rest of the line will be discarded. Try Kiki, a
tool for testing regular expressions, before burying the expression in
your code.

3) Python has built-in booleans: True and False, no quotes. Use them so
that you don't get flamed on forums.

4) I don't claim to be a Python guru, but here's a massaged version of
the function you gave:


def extractCsdInstrument (inputPath, outputPath, instrId):
    """ Takes a .csd file, grabs info for instrument ID and writes to
file. """
    src = open(inputPath, 'r')
    dst = open(outputPath, 'w')
    for line in src:
	if "instr" in line:
	    info_comment = line.split(";", 1) # [info, comment]
            # Extract integers
            instrInfo = re.split(r"[^0-9]*", info_comment[0])
            # Check the second number in the line
            if instrInfo[1] == str(instrId):


I didn't check if this actually works, since I don't know what a .csd
file looks like. There are of course other, better ways to do it, but
hopefully this leads you in the right direction.

5) Please, read the documentation that came with your Python
installation. Reading up on string methods etc. ahead of time will save
you much more time than trying to slug through it. Also consider using
a text editor with syntax highlighting; that will help catch most
obvious syntax errors during coding.

More information about the Python-list mailing list