problem with split

E

Eric_Dexter

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.
 
H

hanumizzle

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.
 
G

goyatlah

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
 
H

hanumizzle

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.

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
 
M

MonkeeSage

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
 
E

Eric_Dexter

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..
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.

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
 
H

hanumizzle

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
 
E

Eric_Dexter

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
 
E

Eric_Dexter

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 ====

I get this but I have at least three lines and the

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




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









Pardon me; I am not very familiar with file format in question.


Cosmetic, but good idea.

-- Theerasak
 
S

Steve Holden

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
 
D

Dennis Lee Bieber

if 'instr' in line:
s = re.split(r' +',line,3)
instr_number = s[1]
return instr_number

Note that since you just return instr_number, you are throwing away
the comment.
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.
.... wrds = line.split()
.... instr_number = wrds[1]
.... comment = " ".join(wrds[2:])
.... .... wrds = line.split()
.... instr_number = wrds[1]
.... comment = " ".join(wrds[2:])
.... --
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
T

Theerasak Photha

hanumizzle wrote:
(snip)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top