473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

problem with split

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
12 1265
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
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
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
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


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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Blue Ocean | last post by:
In short, it's not working right for me. In long: The program is designed to read numbers from an accumulator and speak them out loud. Unfortunately, the class that contains the method to...
14
by: Luka Milkovic | last post by:
Hello, I have a little problem and although it's little it's extremely difficult for me to describe it, but I'll try. I have written a program which extracts certain portions of my received...
3
by: Jan | last post by:
Hi! I have to split a string with a pattern which contains sometimes chars like + \ (the code is needed in an interpreter, written in Perl, of my own scripting language, so I never know the...
12
by: Xah Lee | last post by:
Python Doc Problem Example Quote from: http://docs.python.org/lib/module-os.path.html ---------- split( path) Split the pathname path into a pair, (head, tail) where tail is the last...
8
by: Anthony Liu | last post by:
I have this simple string: mystr = 'this_NP is_VL funny_JJ' I want to split it and give me a list as 1. I tried mystr.split('_| '), but this gave me:
14
by: Ron | last post by:
Hello, I am trying to parse a string on the newline char. I guess vbCrLf is a string constant. How can I parse my string - data - on the newline char? .... data += ASCII.GetString(buffer, 0,...
39
by: Marcin Zmyslowski | last post by:
Hello all! I have the following problem with MS Access 2003 permissions. I have two users. One is admin and the second one is user who has full permissions to enter modify and read data. I...
1
by: Nacho | last post by:
Hello.. I have one problem with my reusable function to validate date.. I want to use this function to all my web project no validate date, but It's works fine if I have one textbox to...
0
by: Iridium | last post by:
Greetings, I am trying to get a JPG Frame from a MJPG Stream. A MJPG is basically a stream of JPGs which are splitted by a special boundary string. So I tried to get the stream, split it by the...
8
by: LayneMitch via WebmasterKB.com | last post by:
I'm supposed to develop a page that asks info as form values and when you hit "submit" it takes you to a page that reads the values you entered into the first page and displays those values in a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.