By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,002 Members | 1,171 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,002 IT Pros & Developers. It's quick & easy.

problem with split

P: n/a
apologies if I annoy and for spacing (google)

def csdInstrumentList(from_file):
"Returns a list of .csd instruments and any comment lines after the
instrument"
infile = open(from_file, 'r')
temp_number = 0
for line in infile:
if 'instr' in line:
s = re.split(r' +',line,3)
instr_number = s[1]
return instr_number

I am coming pretty close to what I want with variations on theis but I
cant seem to
get 3 lines with the split and instr_number[array] = s[1] seems to give
me an error.
the data from the line I am trying to split in three would look like
this

instr 83 ;comment would be here

I want comment returned in an array and instr_number returned in an
array.

Oct 7 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
p.s. this is the one I need to finish to release the csoundroutines
library

www.dexrow.com
Er*********@msn.com wrote:
apologies if I annoy and for spacing (google)

def csdInstrumentList(from_file):
"Returns a list of .csd instruments and any comment lines after the
instrument"
infile = open(from_file, 'r')
temp_number = 0
for line in infile:
if 'instr' in line:
s = re.split(r' +',line,3)
instr_number = s[1]
return instr_number

I am coming pretty close to what I want with variations on theis but I
cant seem to
get 3 lines with the split and instr_number[array] = s[1] seems to give
me an error.
the data from the line I am trying to split in three would look like
this

instr 83 ;comment would be here

I want comment returned in an array and instr_number returned in an
array.
Oct 7 '06 #2

P: n/a
On 6 Oct 2006 21:07:43 -0700, Er*********@msn.com <Er*********@msn.comwrote:
I want comment returned in an array and instr_number returned in an
array.
Let me see if I understand what you want: if there is a line that
starts with instr (best tested with line.startswith('instr') :)), you
want the number and the commen afterwards. I used regexes for this
purpose. In your case:

import re

<snip>

if line.startswith('instr'):
p = re.compile(r'(\d+)\s+;(.*)$')
m = p.search(line)

return (m.group(1), m.group(2))

I returned a tuple of course; you may wish to change that.

BTW, thank you -- I just learned how to use re's in Python.
Oct 7 '06 #3

P: n/a
Think you need a regex like this: regex =
r"\s*instr\s+([0-9]+)\s*(;.*)?"

Then:
import re
test = re.compile(regex)

testing is done as follows:
res = test.match(mystring)
if res:
number = res.group(1) # always a string consisting of decimals
comment = res.group(2) # string starting with ; or None
it might be necessary to have the instr part of the regex as follows
[iI][nN][sS][tT][rR] to handle things like Instr or INSTR.

Hope this helps

Dolf

On 6 Oct 2006 21:07:43 -0700, "Er*********@msn.com"
<Er*********@msn.comwrote:
>apologies if I annoy and for spacing (google)

def csdInstrumentList(from_file):
"Returns a list of .csd instruments and any comment lines after the
instrument"
infile = open(from_file, 'r')
temp_number = 0
for line in infile:
if 'instr' in line:
s = re.split(r' +',line,3)
instr_number = s[1]
return instr_number

I am coming pretty close to what I want with variations on theis but I
cant seem to
get 3 lines with the split and instr_number[array] = s[1] seems to give
me an error.
the data from the line I am trying to split in three would look like
this

instr 83 ;comment would be here

I want comment returned in an array and instr_number returned in an
array.
Oct 7 '06 #4

P: n/a
On 10/7/06, goyatlah <goyatlahwrote:
Think you need a regex like this: regex =
r"\s*instr\s+([0-9]+)\s*(;.*)?"
[0-9] maybe written simply as \d (d for digit)
Then:
import re
test = re.compile(regex)
Regexes are usually passed as literals directly to re.compile().
testing is done as follows:
res = test.match(mystring)
Initial \s* is redundant if you use the search() method.
if res:
I forgot that part. :)
number = res.group(1) # always a string consisting of decimals
comment = res.group(2) # string starting with ; or None
it might be necessary to have the instr part of the regex as follows
[iI][nN][sS][tT][rR] to handle things like Instr or INSTR.
It is sufficient to use the re.IGNORECASE flag.

-- Theerasak
Oct 7 '06 #5

P: n/a


On Oct 6, 11:33 pm, hanumizzle <hanumiz...@gmail.comwrote:
import re

<snip>

if line.startswith('instr'):
p = re.compile(r'(\d+)\s+;(.*)$')
m = p.search(line)

return (m.group(1), m.group(2))
You probably don't want startswith, in case there are initial spaces in
the line. Also, since the regexp is single use, you can just use the
re.search class method, which will compile the regexp implicitly. May
also want to strip the second grouped match, in case of trailing
spaces.

if 'instr' in line:
m = re.search(r'(\d+)\s+;(.*)$', line)
if m:
return (m.group(1), m.group(2).strip())

Regards,
Jordan

Oct 7 '06 #6

P: n/a
I was trying something like this

digits = re.compile("\d")
if digits in line
instr_number = digits.search(line)

because it looked realy cool when I saw it in a recent post... and then
the same thing
for just (';') didn't seem to return anything except one line and some
hex that came from god knows where./... I will see what the other does
and post the result..

hanumizzle wrote:
On 10/7/06, goyatlah <goyatlahwrote:
Think you need a regex like this: regex =
r"\s*instr\s+([0-9]+)\s*(;.*)?"

[0-9] maybe written simply as \d (d for digit)
Then:
import re
test = re.compile(regex)

Regexes are usually passed as literals directly to re.compile().
testing is done as follows:
res = test.match(mystring)

Initial \s* is redundant if you use the search() method.
if res:

I forgot that part. :)
number = res.group(1) # always a string consisting of decimals
comment = res.group(2) # string starting with ; or None
it might be necessary to have the instr part of the regex as follows
[iI][nN][sS][tT][rR] to handle things like Instr or INSTR.

It is sufficient to use the re.IGNORECASE flag.

-- Theerasak
Oct 7 '06 #7

P: n/a
On 6 Oct 2006 23:09:08 -0700, MonkeeSage <Mo********@gmail.comwrote:
>

On Oct 6, 11:33 pm, hanumizzle <hanumiz...@gmail.comwrote:
import re

<snip>

if line.startswith('instr'):
p = re.compile(r'(\d+)\s+;(.*)$')
m = p.search(line)

return (m.group(1), m.group(2))

You probably don't want startswith, in case there are initial spaces in
the line.
Pardon me; I am not very familiar with file format in question.
Also, since the regexp is single use, you can just use the
re.search class method, which will compile the regexp implicitly.
Cool.
May
also want to strip the second grouped match, in case of trailing
spaces.
Cosmetic, but good idea.

-- Theerasak
Oct 7 '06 #8

P: n/a
I think I am very close the return line is tripping me up. (this is
the first list that I have tried to program in python)

return (s.group[1], s.group[2])

Traceback (most recent call last):
File "C:\Python24\Lib\site-packages\boa-constructor\test of
snake\test_of_csoundroutines_list.py", line 5, in ?
v = csoundroutines.csdInstrumentList('bay-at-night.csd')
File "C:\Python24\Lib\site-packages\boa-constructor\test of
snake\csoundroutines.py", line 43, in csdInstrumentList
return (s.group[1], s.group[2])
TypeError: unsubscriptable object




hanumizzle wrote:
On 6 Oct 2006 23:09:08 -0700, MonkeeSage <Mo********@gmail.comwrote:


On Oct 6, 11:33 pm, hanumizzle <hanumiz...@gmail.comwrote:
import re
>
<snip>
>
if line.startswith('instr'):
p = re.compile(r'(\d+)\s+;(.*)$')
m = p.search(line)
>
return (m.group(1), m.group(2))
You probably don't want startswith, in case there are initial spaces in
the line.

Pardon me; I am not very familiar with file format in question.
Also, since the regexp is single use, you can just use the
re.search class method, which will compile the regexp implicitly.

Cool.
May
also want to strip the second grouped match, in case of trailing
spaces.

Cosmetic, but good idea.

-- Theerasak
Oct 7 '06 #9

P: n/a
I am not sure if I am having trouble with the test program or the
routine.. (I had the brackets in the wrong place on the other)

IDLE 1.1.3 ==== No Subprocess ====
>>>
['1', 'String pad']
>>>
I get this but I have at least three lines and the

v = []
v = csoundroutines.csdInstrumentList('bay-at-night.csd')
print v


Er*********@msn.com wrote:
I think I am very close the return line is tripping me up. (this is
the first list that I have tried to program in python)

return (s.group[1], s.group[2])

Traceback (most recent call last):
File "C:\Python24\Lib\site-packages\boa-constructor\test of
snake\test_of_csoundroutines_list.py", line 5, in ?
v = csoundroutines.csdInstrumentList('bay-at-night.csd')
File "C:\Python24\Lib\site-packages\boa-constructor\test of
snake\csoundroutines.py", line 43, in csdInstrumentList
return (s.group[1], s.group[2])
TypeError: unsubscriptable object




hanumizzle wrote:
On 6 Oct 2006 23:09:08 -0700, MonkeeSage <Mo********@gmail.comwrote:
>
>
On Oct 6, 11:33 pm, hanumizzle <hanumiz...@gmail.comwrote:
import re

<snip>

if line.startswith('instr'):
p = re.compile(r'(\d+)\s+;(.*)$')
m = p.search(line)

return (m.group(1), m.group(2))
>
You probably don't want startswith, in case there are initial spaces in
the line.
Pardon me; I am not very familiar with file format in question.
Also, since the regexp is single use, you can just use the
re.search class method, which will compile the regexp implicitly.
Cool.
May
also want to strip the second grouped match, in case of trailing
spaces.
Cosmetic, but good idea.

-- Theerasak
Oct 7 '06 #10

P: n/a
Er*********@msn.com wrote:
I think I am very close the return line is tripping me up. (this is
the first list that I have tried to program in python)

return (s.group[1], s.group[2])

Traceback (most recent call last):
File "C:\Python24\Lib\site-packages\boa-constructor\test of
snake\test_of_csoundroutines_list.py", line 5, in ?
v = csoundroutines.csdInstrumentList('bay-at-night.csd')
File "C:\Python24\Lib\site-packages\boa-constructor\test of
snake\csoundroutines.py", line 43, in csdInstrumentList
return (s.group[1], s.group[2])
TypeError: unsubscriptable object
..group() is a *method of the patch object not a data attribute, so you
have to *call* it, not treat it like a list or dict. Try something like

return (s.group(1), s.group(2))

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Oct 7 '06 #11

P: n/a
hanumizzle wrote:
(snip)
Regexes are usually passed as literals directly to re.compile().
For which definition of "usually" ?

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Oct 9 '06 #12

P: n/a
On 10/9/06, Bruno Desthuilliers <on***@xiludom.growrote:
hanumizzle wrote:
(snip)
Regexes are usually passed as literals directly to re.compile().

For which definition of "usually" ?
>From definition of personal experience: all of the code I've written
or seen that used small regexes in such a context as this wrote them
out literally. Putting such a short string, used only once, in
variable form seems a little verbose unless you have reuse in mind.

IOW, when doing simple arithmetic on paper in an informal context, you
"usually" don't assign the operands to variables before performing the
operations in question.

-- Theerasak
Oct 10 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.