newbe's re question
eric.talevich at gmail.com
Wed Sep 20 09:30:47 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
> def extractCsdInstrument (input_File_Name, output_File_Name,
> "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:`
> reline = re.line.split('instr', '/d$') # ???
> number = int(reline)
> 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...
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
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
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)
# Check the second number in the line
if instrInfo == 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