473,321 Members | 1,669 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,321 software developers and data experts.

newbe question about removing items from one file to another file

def simplecsdtoorc(filename):
file = open(filename,"r")
alllines = file.read_until("</CsInstruments>")
pattern1 = re.compile("</")
orcfilename = filename[-3:] + "orc"
for line in alllines:
if not pattern1
print >>orcfilename, line

I am pretty sure my code isn't close to what I want. I need to be able
to skip html like commands from <definedto <undefinedand to key on
another word in adition to </CsInstrumentsto end the routine

I was also looking at se 2.2 beta but didn't see any easy way to use it
for this or for that matter search and replace where I could just add
it as a menu item and not worry about it.

thanks for any help in advance

Aug 27 '06 #1
17 2669
Sounds like you need to use html parser, check it out in the
documentation....

<Er*********@msn.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
def simplecsdtoorc(filename):
file = open(filename,"r")
alllines = file.read_until("</CsInstruments>")
pattern1 = re.compile("</")
orcfilename = filename[-3:] + "orc"
for line in alllines:
if not pattern1
print >>orcfilename, line

I am pretty sure my code isn't close to what I want. I need to be able
to skip html like commands from <definedto <undefinedand to key on
another word in adition to </CsInstrumentsto end the routine

I was also looking at se 2.2 beta but didn't see any easy way to use it
for this or for that matter search and replace where I could just add
it as a menu item and not worry about it.

thanks for any help in advance

Aug 27 '06 #2

PetDragon wrote:
Sounds like you need to use html parser, check it out in the
documentation....

<Er*********@msn.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
def simplecsdtoorc(filename):
file = open(filename,"r")
alllines = file.read_until("</CsInstruments>")
pattern1 = re.compile("</")
orcfilename = filename[-3:] + "orc"
for line in alllines:
if not pattern1
print >>orcfilename, line

I am pretty sure my code isn't close to what I want. I need to be able
to skip html like commands from <definedto <undefinedand to key on
another word in adition to </CsInstrumentsto end the routine

I was also looking at se 2.2 beta but didn't see any easy way to use it
for this or for that matter search and replace where I could just add
it as a menu item and not worry about it.

thanks for any help in advance
I will look into that a little bit since that is so html like... maybe
some of the examples can lead me in the right direction on alot of it..

http://www.dexrow.com

Aug 28 '06 #3
Er*********@msn.com wrote:
def simplecsdtoorc(filename):
file = open(filename,"r")
alllines = file.read_until("</CsInstruments>")
pattern1 = re.compile("</")
orcfilename = filename[-3:] + "orc"
for line in alllines:
if not pattern1
print >>orcfilename, line

I am pretty sure my code isn't close to what I want. I need to be able
to skip html like commands from <definedto <undefinedand to key on
another word in adition to </CsInstrumentsto end the routine

I was also looking at se 2.2 beta but didn't see any easy way to use it
for this or for that matter search and replace where I could just add
it as a menu item and not worry about it.

thanks for any help in advance
If you're dealing with html or html-like files, do check out
beautifulsoup. I had reason to use it the other day and man is it ever
useful!

Meantime, there are a few minor points about the code you posted:

1) open() defaults to 'r', you can leave it out when you call open() to
read a file.

2) 'file' is a builtin type (it's the type of file objects returned by
open()) so you shouldn't use it as a variable name.

3) file objects don't have a read_until() method. You could say
something like:

f = open(filename)
lines = []
for line in f:
lines.append(line)
if '</CsInstruments>' in line:
break

4) filename[-3:] will give you the last 3 chars in filename. I'm
guessing that you want all but the last 3 chars, that's filename[:-3],
but see the os.path.splitext() function, and indeed the other
functions in os.path too:
http://docs.python.org/lib/module-os.path.html

5) the regular expression objects returned by re.compile() will always
evaluate True, so you want to call their search() method on the data to
search:

if not pattern1.search(line):

But, 6) using re for a pattern as simple as "</" is way overkill. Just
use 'in' or the find() method of strings:

if "</" not in line:

or:

pos = line.find("</")
if pos == -1:
print >>orcfilename, line
else:
print >>orcfilename, line[:pos]

7) the "print >file" usage requires a file (or file-like object,
anything with a write() method I think) not a string. You need to use
it like this:

orcfile = open(orcfilename, 'w')
#...
print >orcfile, line

8) If you have a list of lines anyway, you can use the writelines()
method of files to write them in one go:

open(orcfilename, 'w').writelines(lines)

of course stripping out your unwanted data from that last line using
find() as shown above.

I hope this helps.

Check out the docs on file objects:
http://docs.python.org/lib/bltin-file-objects.html, but like I said,
if you're dealing with html or html-like files, be sure to check out
beautifulsoup. Also, there's the elementtree package for parsing XML
that could help here too.

~Simon

Aug 28 '06 #4
Eric,
Having played around with problems of this kind for quite some time I find them challenging even if I don't really have time to
get sidetracked. Your description of the problem makes it all the more challenging, because its 'expressionist' quality adds the
challenge of guessing what you mean.
I'd like to take a look at your data, if you would post a segment on which to operate, the same data the way it should look in
the end. In most cases this is pretty self-explanatory. Explain the points that might not be obvious to a reader who knows nothing
about your mission.

Frederic

----- Original Message -----
From: <Er*********@msn.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Sunday, August 27, 2006 11:35 PM
Subject: newbe question about removing items from one file to another file

def simplecsdtoorc(filename):
file = open(filename,"r")
alllines = file.read_until("</CsInstruments>")
pattern1 = re.compile("</")
orcfilename = filename[-3:] + "orc"
for line in alllines:
if not pattern1
print >>orcfilename, line

I am pretty sure my code isn't close to what I want. I need to be able
to skip html like commands from <definedto <undefinedand to key on
another word in adition to </CsInstrumentsto end the routine

I was also looking at se 2.2 beta but didn't see any easy way to use it
for this or for that matter search and replace where I could just add
it as a menu item and not worry about it.

thanks for any help in advance

--
http://mail.python.org/mailman/listinfo/python-list
Aug 28 '06 #5

Anthra Norell wrote:
Eric,
Having played around with problems of this kind for quite some time I find them challenging even if I don't really have time to
get sidetracked. Your description of the problem makes it all the more challenging, because its 'expressionist' quality adds the
challenge of guessing what you mean.
I'd like to take a look at your data, if you would post a segment on which to operate, the same data the way it should look in
the end. In most cases this is pretty self-explanatory. Explain the points that might not be obvious to a reader who knows nothing
about your mission.

Frederic

----- Original Message -----
From: <Er*********@msn.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Sunday, August 27, 2006 11:35 PM
Subject: newbe question about removing items from one file to another file

def simplecsdtoorc(filename):
file = open(filename,"r")
alllines = file.read_until("</CsInstruments>")
pattern1 = re.compile("</")
orcfilename = filename[-3:] + "orc"
for line in alllines:
if not pattern1
print >>orcfilename, line

I am pretty sure my code isn't close to what I want. I need to be able
to skip html like commands from <definedto <undefinedand to key on
another word in adition to </CsInstrumentsto end the routine

I was also looking at se 2.2 beta but didn't see any easy way to use it
for this or for that matter search and replace where I could just add
it as a menu item and not worry about it.

thanks for any help in advance

--
http://mail.python.org/mailman/listinfo/python-list
sorry about that this is a link to a discription of the format
http://kevindumpscore.com/docs/csoun...ndunifile.html
It is possible to have more than one instr defined in an .csd file so I
would need to look for that string also if I want to seperate the
instruments out.

http://www.dexrow.com

Aug 28 '06 #6
At Sunday 27/8/2006 18:35, Er*********@msn.com wrote:

(This code don't even compile...!)
>def simplecsdtoorc(filename):
file = open(filename,"r")
file is not a good name - hides the builtin type of the same name.
Same for dict, list...
alllines = file.read_until("</CsInstruments>")
read_until???
pattern1 = re.compile("</")
orcfilename = filename[-3:] + "orc"
perhaps you want filename[:-3]+"orc"?
for line in alllines:
if not pattern1
if not pattern1.search(line):
print >>orcfilename, line
Open the output file before the loop, and use its write() method here
>I am pretty sure my code isn't close to what I want. I need to be able
to skip html like commands from <definedto <undefinedand to key on
another word in adition to </CsInstrumentsto end the routine
Good job for Beautiful Soup: http://www.crummy.com/software/BeautifulSoup/

Gabriel Genellina
Softlab SRL

__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Aug 29 '06 #7
Dexter,

I looked at the format specification. It contains an example:

-----------------------------------------------

<CsoundSynthesizer>;
; test.csd - a Csound structured data file

<CsOptions>
-W -d -o tone.wav
</CsOptions>

<CsVersion ;optional section
Before 4.10 ;these two statements check for
After 4.08 ; Csound version 4.09
</CsVersion>

<CsInstruments>
; originally tone.orc
sr = 44100
kr = 4410
ksmps = 10
nchnls = 1
instr 1
a1 oscil p4, p5, 1 ; simple oscillator
out a1
endin
</CsInstruments>

<CsScore>
; originally tone.sco
f1 0 8192 10 1
i1 0 1 20000 1000 ;play one second of one kHz tone
e
</CsScore>

</CsoundSynthesizer>

-------------------------------------

If I understand correctly you want to write the instruments block to a file (from <CsInstrumentsto </CsInstruments>)? Right? Or
each block to its own file in case there are several?. You want your code to generate the file names? Can you confirm this or
explain it differently?

Regards

Frederic
----- Original Message -----
From: <Er*********@msn.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Monday, August 28, 2006 10:48 AM
Subject: Re: newbe question about removing items from one file to another file

>
Anthra Norell wrote:
Eric,
Having played around with problems of this kind for quite some time I find them challenging even if I don't really have time
to
get sidetracked. Your description of the problem makes it all the more challenging, because its 'expressionist' quality adds the
challenge of guessing what you mean.
I'd like to take a look at your data, if you would post a segment on which to operate, the same data the way it should look
in
the end. In most cases this is pretty self-explanatory. Explain the points that might not be obvious to a reader who knows
nothing
about your mission.

Frederic

----- Original Message -----
From: <Er*********@msn.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Sunday, August 27, 2006 11:35 PM
Subject: newbe question about removing items from one file to another file

def simplecsdtoorc(filename):
file = open(filename,"r")
alllines = file.read_until("</CsInstruments>")
pattern1 = re.compile("</")
orcfilename = filename[-3:] + "orc"
for line in alllines:
if not pattern1
print >>orcfilename, line
>
I am pretty sure my code isn't close to what I want. I need to be able
to skip html like commands from <definedto <undefinedand to key on
another word in adition to </CsInstrumentsto end the routine
>
I was also looking at se 2.2 beta but didn't see any easy way to use it
for this or for that matter search and replace where I could just add
it as a menu item and not worry about it.
>
thanks for any help in advance
>
--
http://mail.python.org/mailman/listinfo/python-list

sorry about that this is a link to a discription of the format
http://kevindumpscore.com/docs/csoun...ndunifile.html
It is possible to have more than one instr defined in an .csd file so I
would need to look for that string also if I want to seperate the
instruments out.

http://www.dexrow.com

--
http://mail.python.org/mailman/listinfo/python-list
Aug 29 '06 #8

Anthra Norell wrote:
Dexter,

I looked at the format specification. It contains an example:

-----------------------------------------------

<CsoundSynthesizer>;
; test.csd - a Csound structured data file

<CsOptions>
-W -d -o tone.wav
</CsOptions>

<CsVersion ;optional section
Before 4.10 ;these two statements check for
After 4.08 ; Csound version 4.09
</CsVersion>

<CsInstruments>
; originally tone.orc
sr = 44100
kr = 4410
ksmps = 10
nchnls = 1
instr 1
a1 oscil p4, p5, 1 ; simple oscillator
out a1
endin
</CsInstruments>

<CsScore>
; originally tone.sco
f1 0 8192 10 1
i1 0 1 20000 1000 ;play one second of one kHz tone
e
</CsScore>

</CsoundSynthesizer>

-------------------------------------

If I understand correctly you want to write the instruments block to a file (from <CsInstrumentsto </CsInstruments>)? Right? Or
each block to its own file in case there are several?. You want your code to generate the file names? Can you confirm this or
explain it differently?

Regards

Frederic
----- Original Message -----
From: <Er*********@msn.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Monday, August 28, 2006 10:48 AM
Subject: Re: newbe question about removing items from one file to another file


Anthra Norell wrote:
Eric,
Having played around with problems of this kind for quite some time I find them challenging even if I don't really have time
to
get sidetracked. Your description of the problem makes it all the more challenging, because its 'expressionist' quality adds the
challenge of guessing what you mean.
I'd like to take a look at your data, if you would post a segment on which to operate, the same data the way it should look
in
the end. In most cases this is pretty self-explanatory. Explain the points that might not be obvious to a reader who knows
nothing
about your mission.
>
Frederic
>
----- Original Message -----
From: <Er*********@msn.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Sunday, August 27, 2006 11:35 PM
Subject: newbe question about removing items from one file to another file
>
>
def simplecsdtoorc(filename):
file = open(filename,"r")
alllines = file.read_until("</CsInstruments>")
pattern1 = re.compile("</")
orcfilename = filename[-3:] + "orc"
for line in alllines:
if not pattern1
print >>orcfilename, line

I am pretty sure my code isn't close to what I want. I need to be able
to skip html like commands from <definedto <undefinedand to key on
another word in adition to </CsInstrumentsto end the routine

I was also looking at se 2.2 beta but didn't see any easy way to use it
for this or for that matter search and replace where I could just add
it as a menu item and not worry about it.

thanks for any help in advance

--
http://mail.python.org/mailman/listinfo/python-list
sorry about that this is a link to a discription of the format
http://kevindumpscore.com/docs/csoun...ndunifile.html
It is possible to have more than one instr defined in an .csd file so I
would need to look for that string also if I want to seperate the
instruments out.

http://www.dexrow.com

--
http://mail.python.org/mailman/listinfo/python-list
I need to take it between the blocks only I also need to make sure I
only take one instrument
defined in this example with the code instr 1 I also need the code

<CsInstruments>
; originally tone.orc
sr = 44100
kr = 4410
ksmps = 10
nchnls = 1
regardless of what instrument I take. The function would have to
except the instrument number as an argument

http://www.dexrow.com

Aug 29 '06 #9
Er*********@msn.com wrote:
Anthra Norell wrote:
Dexter,

I looked at the format specification. It contains an example:

-----------------------------------------------

<CsoundSynthesizer>;
; test.csd - a Csound structured data file

<CsOptions>
-W -d -o tone.wav
</CsOptions>

<CsVersion ;optional section
Before 4.10 ;these two statements check for
After 4.08 ; Csound version 4.09
</CsVersion>

<CsInstruments>
; originally tone.orc
sr = 44100
kr = 4410
ksmps = 10
nchnls = 1
instr 1
a1 oscil p4, p5, 1 ; simple oscillator
out a1
endin
</CsInstruments>

<CsScore>
; originally tone.sco
f1 0 8192 10 1
i1 0 1 20000 1000 ;play one second of one kHz tone
e
</CsScore>

</CsoundSynthesizer>

-------------------------------------

If I understand correctly you want to write the instruments block to a file (from <CsInstrumentsto </CsInstruments>)? Right? Or
each block to its own file in case there are several?. You want your code to generate the file names? Can you confirm this or
explain it differently?

Regards

Frederic
----- Original Message -----
From: <Er*********@msn.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Monday, August 28, 2006 10:48 AM
Subject: Re: newbe question about removing items from one file to another file

>
Anthra Norell wrote:
Eric,
Having played around with problems of this kind for quite some time I find them challenging even if I don't really have time
to
get sidetracked. Your description of the problem makes it all the more challenging, because its 'expressionist' quality adds the
challenge of guessing what you mean.
I'd like to take a look at your data, if you would post a segment on which to operate, the same data the way it should look
in
the end. In most cases this is pretty self-explanatory. Explain the points that might not be obvious to a reader who knows
nothing
about your mission.

Frederic

----- Original Message -----
From: <Er*********@msn.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Sunday, August 27, 2006 11:35 PM
Subject: newbe question about removing items from one file to another file


def simplecsdtoorc(filename):
file = open(filename,"r")
alllines = file.read_until("</CsInstruments>")
pattern1 = re.compile("</")
orcfilename = filename[-3:] + "orc"
for line in alllines:
if not pattern1
print >>orcfilename, line
>
I am pretty sure my code isn't close to what I want. I need to be able
to skip html like commands from <definedto <undefinedand to key on
another word in adition to </CsInstrumentsto end the routine
>
I was also looking at se 2.2 beta but didn't see any easy way to use it
for this or for that matter search and replace where I could just add
it as a menu item and not worry about it.
>
thanks for any help in advance
>
--
http://mail.python.org/mailman/listinfo/python-list
>
sorry about that this is a link to a discription of the format
http://kevindumpscore.com/docs/csoun...ndunifile.html
It is possible to have more than one instr defined in an .csd file so I
would need to look for that string also if I want to seperate the
instruments out.
>
http://www.dexrow.com
>
--
http://mail.python.org/mailman/listinfo/python-list

I need to take it between the blocks only I also need to make sure I
only take one instrument
defined in this example with the code instr 1 I also need the code

<CsInstruments>
; originally tone.orc
sr = 44100
kr = 4410
ksmps = 10
nchnls = 1

regardless of what instrument I take. The function would have to
except the instrument number as an argument

http://www.dexrow.com
Using BeautifulSoup and the interactive interpreter, I figured out the
following script in about 15 minutes:

# s is a string containing the example file from above.

from BeautifulSoup import BeautifulStoneSoup

soup = BeautifulStoneSoup(s)
csin = soup.contents[0].contents[5]
lines = csin.string.splitlines()

print csin.string

It prints:

; originally tone.orc
sr = 44100
kr = 4410
ksmps = 10
nchnls = 1
instr 1
a1 oscil p4, p5, 1 ; simple oscillator
out a1
endin
and of course you could say "lines = csin.string.splitlines()" to get a
list of the lines. That doesn't take you all the way, but it's
something.

Hope that helps,
Peace,
~Simon

Aug 30 '06 #10
Dexter,

Here's a function that screens out all instrument blocks and puts them into a dictionary keyed on the instrument number:

--------------------------------------------

def get_instruments (file_name):

INSIDE = 1
OUTSIDE = 0

f = file (file_name, 'ra')
state = OUTSIDE
instruments = {}
instrument_segment = ''

for line in f:
if state == OUTSIDE:
if line.startswith ('<CsInstruments'):
state = INSIDE
instrument_segment += line
else:
instrument_segment += line
if line.lstrip ().startswith ('instr'):
instrument_number = line.split () [1]
elif line.startswith ('</CsInstruments'):
instruments [instrument_number] = instrument_segment
instrument_segment = ''
state = OUTSIDE

f.close ()
return instruments

------------------------------------------------

You have received good advice on using parsers: "beautiful soup" or "pyparse". These are powerful tools capable of doing complicated
extractions. Yours is not a complicated extraction. Simon tried it with "beautiful soup". That seems simple enough, though he finds
the data by index leaving open where he gets the index from. There's surely a way to get the data by name.
Contrary to the parser the function will miss if tags take liberties with upper-lower case letters as they are probably
allowed by the specification. A regular expression might have to be used, if they do.
From your description I haven't been able to infer what the final format of your data is supposed to be. So I cannot tell you
how to go on from here. You'll find out. If not, just keep asking.

The SE solution which you said couldn't work out would be the following. It makes the same dictionary the function makes and it is
case-insensitive:

------------------------------------------------
>>Instrument_Segment_Filter = SE.SE ('<EAT"~(?i)<CsInstruments>(.|\n)*?</CsInstruments>~==\n\n" ')
instrument_segments= Instrument_Segment_Filter ('file_name', '')
print instrument_segments
(... see all instrument segments ...)
>>Instrument_Number = SE.SE ('<EAT~instr.*~==\n')
instruments ={}
for segment in instrument_segments.split ('\n\n'):
if segment:
instr_line = Instrument_Number (segment)
instrument_number = instr_line.split ()[1]
instruments [instrument_number] = segment

--------------------------------------------------

(If you're on Windows and the CRs bother you, take them out with an additional definition when you make your
Instrument_Block_Filter: (13)= or "\r=")
Regards

Frederic
----- Original Message -----
From: <Er*********@msn.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Wednesday, August 30, 2006 1:51 AM
Subject: Re: newbe question about removing items from one file to another file

>
Anthra Norell wrote:
Dexter,

I looked at the format specification. It contains an example:

-----------------------------------------------

<CsoundSynthesizer>;
; test.csd - a Csound structured data file

<CsOptions>
-W -d -o tone.wav
</CsOptions>
....
etc.

Aug 30 '06 #11

Anthra Norell wrote:
Dexter,

Here's a function that screens out all instrument blocks and puts them into a dictionary keyed on the instrument number:

--------------------------------------------

def get_instruments (file_name):

INSIDE = 1
OUTSIDE = 0

f = file (file_name, 'ra')
state = OUTSIDE
instruments = {}
instrument_segment = ''

for line in f:
if state == OUTSIDE:
if line.startswith ('<CsInstruments'):
state = INSIDE
instrument_segment += line
else:
instrument_segment += line
if line.lstrip ().startswith ('instr'):
instrument_number = line.split () [1]
elif line.startswith ('</CsInstruments'):
instruments [instrument_number] = instrument_segment
instrument_segment = ''
state = OUTSIDE

f.close ()
return instruments

------------------------------------------------

You have received good advice on using parsers: "beautiful soup" or "pyparse". These are powerful tools capable of doing complicated
extractions. Yours is not a complicated extraction. Simon tried it with "beautiful soup". That seems simple enough, though he finds
the data by index leaving open where he gets the index from. There's surely a way to get the data by name.
Contrary to the parser the function will miss if tags take liberties with upper-lower case letters as they are probably
allowed by the specification. A regular expression might have to be used, if they do.
From your description I haven't been able to infer what the final format of your data is supposed to be. So I cannot tell you
how to go on from here. You'll find out. If not, just keep asking.

The SE solution which you said couldn't work out would be the following. It makes the same dictionary the function makes and it is
case-insensitive:

------------------------------------------------
>Instrument_Segment_Filter = SE.SE ('<EAT"~(?i)<CsInstruments>(.|\n)*?</CsInstruments>~==\n\n" ')
instrument_segments= Instrument_Segment_Filter ('file_name', '')
print instrument_segments
(... see all instrument segments ...)
>Instrument_Number = SE.SE ('<EAT~instr.*~==\n')
instruments ={}
for segment in instrument_segments.split ('\n\n'):
if segment:
instr_line = Instrument_Number (segment)
instrument_number = instr_line.split ()[1]
instruments [instrument_number] = segment

--------------------------------------------------

(If you're on Windows and the CRs bother you, take them out with an additional definition when you make your
Instrument_Block_Filter: (13)= or "\r=")
Regards

Frederic
----- Original Message -----
From: <Er*********@msn.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Wednesday, August 30, 2006 1:51 AM
Subject: Re: newbe question about removing items from one file to another file


Anthra Norell wrote:
Dexter,
>
I looked at the format specification. It contains an example:
>
-----------------------------------------------
>
<CsoundSynthesizer>;
; test.csd - a Csound structured data file
>
<CsOptions>
-W -d -o tone.wav
</CsOptions>
>
...
etc.
Thanks for the help I can't wait to try it out.. (has to wait for the
weekend.. three days off finaly.)

http://www.dexrow.com

Aug 31 '06 #12

Simon Forman wrote:
Er*********@msn.com wrote:
Anthra Norell wrote:
Dexter,
>
I looked at the format specification. It contains an example:
>
-----------------------------------------------
>
<CsoundSynthesizer>;
; test.csd - a Csound structured data file
>
<CsOptions>
-W -d -o tone.wav
</CsOptions>
>
<CsVersion ;optional section
Before 4.10 ;these two statements check for
After 4.08 ; Csound version 4.09
</CsVersion>
>
<CsInstruments>
; originally tone.orc
sr = 44100
kr = 4410
ksmps = 10
nchnls = 1
instr 1
a1 oscil p4, p5, 1 ; simple oscillator
out a1
endin
</CsInstruments>
>
<CsScore>
; originally tone.sco
f1 0 8192 10 1
i1 0 1 20000 1000 ;play one second of one kHz tone
e
</CsScore>
>
</CsoundSynthesizer>
>
-------------------------------------
>
If I understand correctly you want to write the instruments block to a file (from <CsInstrumentsto </CsInstruments>)? Right? Or
each block to its own file in case there are several?. You want your code to generate the file names? Can you confirm this or
explain it differently?
>
Regards
>
Frederic
>
>
----- Original Message -----
From: <Er*********@msn.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Monday, August 28, 2006 10:48 AM
Subject: Re: newbe question about removing items from one file to another file
>
>

Anthra Norell wrote:
Eric,
Having played around with problems of this kind for quite some time I find them challenging even if I don't really have time
to
get sidetracked. Your description of the problem makes it all the more challenging, because its 'expressionist' quality adds the
challenge of guessing what you mean.
I'd like to take a look at your data, if you would post a segment on which to operate, the same data the way it should look
in
the end. In most cases this is pretty self-explanatory. Explain the points that might not be obvious to a reader who knows
nothing
about your mission.
>
Frederic
>
----- Original Message -----
From: <Er*********@msn.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Sunday, August 27, 2006 11:35 PM
Subject: newbe question about removing items from one file to another file
>
>
def simplecsdtoorc(filename):
file = open(filename,"r")
alllines = file.read_until("</CsInstruments>")
pattern1 = re.compile("</")
orcfilename = filename[-3:] + "orc"
for line in alllines:
if not pattern1
print >>orcfilename, line

I am pretty sure my code isn't close to what I want. I need to be able
to skip html like commands from <definedto <undefinedand to key on
another word in adition to </CsInstrumentsto end the routine

I was also looking at se 2.2 beta but didn't see any easy way to use it
for this or for that matter search and replace where I could just add
it as a menu item and not worry about it.

thanks for any help in advance

--
http://mail.python.org/mailman/listinfo/python-list

sorry about that this is a link to a discription of the format
http://kevindumpscore.com/docs/csoun...ndunifile.html
It is possible to have more than one instr defined in an .csd file so I
would need to look for that string also if I want to seperate the
instruments out.

http://www.dexrow.com

--
http://mail.python.org/mailman/listinfo/python-list
I need to take it between the blocks only I also need to make sure I
only take one instrument
defined in this example with the code instr 1 I also need the code

<CsInstruments>
; originally tone.orc
sr = 44100
kr = 4410
ksmps = 10
nchnls = 1
regardless of what instrument I take. The function would have to
except the instrument number as an argument

http://www.dexrow.com

Using BeautifulSoup and the interactive interpreter, I figured out the
following script in about 15 minutes:

# s is a string containing the example file from above.

from BeautifulSoup import BeautifulStoneSoup

soup = BeautifulStoneSoup(s)
csin = soup.contents[0].contents[5]
lines = csin.string.splitlines()

print csin.string

It prints:

; originally tone.orc
sr = 44100
kr = 4410
ksmps = 10
nchnls = 1
instr 1
a1 oscil p4, p5, 1 ; simple oscillator
out a1
endin
and of course you could say "lines = csin.string.splitlines()" to get a
list of the lines. That doesn't take you all the way, but it's
something.

Hope that helps,
Peace,
~Simon
I seem to be having problems getting the code to work.. Seems to crash
my whole project, I don't know if I am missing an import file or what
(I had to go back to an older version on my hd.. I have uploaded what
I have on to sourceforge

https://sourceforge.net/project/show...ease_id=444362
http://www.dexrow.com

thanks for the help

Sep 4 '06 #13

Anthra Norell wrote:
Dexter,

I looked at the format specification. It contains an example:

-----------------------------------------------

<CsoundSynthesizer>;
; test.csd - a Csound structured data file

<CsOptions>
-W -d -o tone.wav
</CsOptions>

<CsVersion ;optional section
Before 4.10 ;these two statements check for
After 4.08 ; Csound version 4.09
</CsVersion>

<CsInstruments>
; originally tone.orc
sr = 44100
kr = 4410
ksmps = 10
nchnls = 1
instr 1
a1 oscil p4, p5, 1 ; simple oscillator
out a1
endin
</CsInstruments>

<CsScore>
; originally tone.sco
f1 0 8192 10 1
i1 0 1 20000 1000 ;play one second of one kHz tone
e
</CsScore>

</CsoundSynthesizer>

-------------------------------------

If I understand correctly you want to write the instruments block to a file (from <CsInstrumentsto </CsInstruments>)? Right? Or
each block to its own file in case there are several?. You want your code to generate the file names? Can you confirm this or
explain it differently?

Regards

Frederic
----- Original Message -----
From: <Er*********@msn.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Monday, August 28, 2006 10:48 AM
Subject: Re: newbe question about removing items from one file to another file


Anthra Norell wrote:
Eric,
Having played around with problems of this kind for quite some time I find them challenging even if I don't really have time
to
get sidetracked. Your description of the problem makes it all the more challenging, because its 'expressionist' quality adds the
challenge of guessing what you mean.
I'd like to take a look at your data, if you would post a segment on which to operate, the same data the way it should look
in
the end. In most cases this is pretty self-explanatory. Explain the points that might not be obvious to a reader who knows
nothing
about your mission.
>
Frederic
>
----- Original Message -----
From: <Er*********@msn.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Sunday, August 27, 2006 11:35 PM
Subject: newbe question about removing items from one file to another file
>
>
def simplecsdtoorc(filename):
file = open(filename,"r")
alllines = file.read_until("</CsInstruments>")
pattern1 = re.compile("</")
orcfilename = filename[-3:] + "orc"
for line in alllines:
if not pattern1
print >>orcfilename, line

I am pretty sure my code isn't close to what I want. I need to be able
to skip html like commands from <definedto <undefinedand to key on
another word in adition to </CsInstrumentsto end the routine

I was also looking at se 2.2 beta but didn't see any easy way to use it
for this or for that matter search and replace where I could just add
it as a menu item and not worry about it.

thanks for any help in advance

--
http://mail.python.org/mailman/listinfo/python-list
sorry about that this is a link to a discription of the format
http://kevindumpscore.com/docs/csoun...ndunifile.html
It is possible to have more than one instr defined in an .csd file so I
would need to look for that string also if I want to seperate the
instruments out.

http://www.dexrow.com

--
http://mail.python.org/mailman/listinfo/python-list
sorry I responded to the wrong post... I was having trouble figuring
out the buitiful soup download

Sep 4 '06 #14

Anthra Norell wrote:
Dexter,

Here's a function that screens out all instrument blocks and puts them into a dictionary keyed on the instrument number:

--------------------------------------------

def get_instruments (file_name):

INSIDE = 1
OUTSIDE = 0

f = file (file_name, 'ra')
state = OUTSIDE
instruments = {}
instrument_segment = ''

for line in f:
if state == OUTSIDE:
if line.startswith ('<CsInstruments'):
state = INSIDE
instrument_segment += line
else:
instrument_segment += line
if line.lstrip ().startswith ('instr'):
instrument_number = line.split () [1]
elif line.startswith ('</CsInstruments'):
instruments [instrument_number] = instrument_segment
instrument_segment = ''
state = OUTSIDE

f.close ()
return instruments

------------------------------------------------

You have received good advice on using parsers: "beautiful soup" or "pyparse". These are powerful tools capable of doing complicated
extractions. Yours is not a complicated extraction. Simon tried it with "beautiful soup". That seems simple enough, though he finds
the data by index leaving open where he gets the index from. There's surely a way to get the data by name.
Contrary to the parser the function will miss if tags take liberties with upper-lower case letters as they are probably
allowed by the specification. A regular expression might have to be used, if they do.
From your description I haven't been able to infer what the final format of your data is supposed to be. So I cannot tell you
how to go on from here. You'll find out. If not, just keep asking.

The SE solution which you said couldn't work out would be the following. It makes the same dictionary the function makes and it is
case-insensitive:

------------------------------------------------
>Instrument_Segment_Filter = SE.SE ('<EAT"~(?i)<CsInstruments>(.|\n)*?</CsInstruments>~==\n\n" ')
instrument_segments= Instrument_Segment_Filter ('file_name', '')
print instrument_segments
(... see all instrument segments ...)
>Instrument_Number = SE.SE ('<EAT~instr.*~==\n')
instruments ={}
for segment in instrument_segments.split ('\n\n'):
if segment:
instr_line = Instrument_Number (segment)
instrument_number = instr_line.split ()[1]
instruments [instrument_number] = segment

--------------------------------------------------

(If you're on Windows and the CRs bother you, take them out with an additional definition when you make your
Instrument_Block_Filter: (13)= or "\r=")
Regards

Frederic
----- Original Message -----
From: <Er*********@msn.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Wednesday, August 30, 2006 1:51 AM
Subject: Re: newbe question about removing items from one file to another file


Anthra Norell wrote:
Dexter,
>
I looked at the format specification. It contains an example:
>
-----------------------------------------------
>
<CsoundSynthesizer>;
; test.csd - a Csound structured data file
>
<CsOptions>
-W -d -o tone.wav
</CsOptions>
>
...
etc.
I cut and pasted this.. It seems to be crashing my program.. I am not
sure that I have all the right imports.. seems to be fine when I go to
an older version of the file... I uploaded it onto source forge.

https://sourceforge.net/project/show...ease_id=444362
http://www.dexrow.com

Sep 4 '06 #15

----- Original Message -----
From: <Er*********@msn.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Monday, September 04, 2006 4:58 AM
Subject: Re: newbe question about removing items from one file to another file

>
Anthra Norell wrote:
Dexter,

Here's a function that screens out all instrument blocks and puts them into a dictionary keyed on the instrument number:

--------------------------------------------

def get_instruments (file_name):
etc.
<CsOptions>
-W -d -o tone.wav
</CsOptions>
...
etc.

I cut and pasted this.. It seems to be crashing my program.. I am not
sure that I have all the right imports.. seems to be fine when I go to
an older version of the file... I uploaded it onto source forge.

https://sourceforge.net/project/show...ease_id=444362
http://www.dexrow.com
Eric (Eric or Dexer?)
This thread seems to have split. So let me reiterate: please copy the output when you cut, paste and run. If you have an
import problem it must be on the other side of your interface with SE, because I don't import anything and SE imports what it needs.

Frederic
Sep 4 '06 #16
I am have to be able to distribute se with the project in order to use
it
I started with import se but I did not use the setup command
when I comment out import se the program works and when
I use import se everything connected to the library crashes on the
import line..


Anthra Norell wrote:
----- Original Message -----
From: <Er*********@msn.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Monday, September 04, 2006 4:58 AM
Subject: Re: newbe question about removing items from one file to another file


Anthra Norell wrote:
Dexter,
>
Here's a function that screens out all instrument blocks and puts them into a dictionary keyed on the instrument number:
>
--------------------------------------------
>
def get_instruments (file_name):

etc.
<CsOptions>
-W -d -o tone.wav
</CsOptions>
>
...
etc.
I cut and pasted this.. It seems to be crashing my program.. I am not
sure that I have all the right imports.. seems to be fine when I go to
an older version of the file... I uploaded it onto source forge.

https://sourceforge.net/project/show...ease_id=444362
http://www.dexrow.com

Eric (Eric or Dexer?)
This thread seems to have split. So let me reiterate: please copy the output when you cut, paste and run. If you have an
import problem it must be on the other side of your interface with SE, because I don't import anything and SE imports what it needs.

Frederic
Sep 4 '06 #17
You don't need the setup command. Just place SE.py and SEL.py into a path where the import can find it. Also make sure SE.py and
SEL.py are spelled exactly like this. Linux requires the extension to be lower case, as I was myself made aware of by an alert
person who was also experiencing import problems. I must confess that my fist uploads were upper case (SE.PY). I instantaneously
replaced the upload with corrected spelling and apologize for the trouble the mistake may be causing. Fortunately correcting it is a
small matter.
Have you tried to run the function at all? It produces the same result. Made case-insensitive (if need be) I'd prefer the
function. It is more economical, since it doesn't require an extra import. It surely runs faster too (if that matters).

Frederic

----- Original Message -----
From: <Er*********@msn.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Monday, September 04, 2006 10:52 PM
Subject: Re: newbe question about removing items from one file to another file

I am have to be able to distribute se with the project in order to use
it
I started with import se but I did not use the setup command
when I comment out import se the program works and when
I use import se everything connected to the library crashes on the
import line..


Anthra Norell wrote:
----- Original Message -----
From: <Er*********@msn.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Monday, September 04, 2006 4:58 AM
Subject: Re: newbe question about removing items from one file to another file

>
Anthra Norell wrote:
Dexter,

Here's a function that screens out all instrument blocks and puts them into a dictionary keyed on the instrument number:

--------------------------------------------

def get_instruments (file_name):
etc.
<CsOptions>
-W -d -o tone.wav
</CsOptions>

...
etc.
>
I cut and pasted this.. It seems to be crashing my program.. I am not
sure that I have all the right imports.. seems to be fine when I go to
an older version of the file... I uploaded it onto source forge.
>
https://sourceforge.net/project/show...ease_id=444362
http://www.dexrow.com
>
Eric (Eric or Dexer?)
This thread seems to have split. So let me reiterate: please copy the output when you cut, paste and run. If you have an
import problem it must be on the other side of your interface with SE, because I don't import anything and SE imports what it
needs.

Frederic

--
http://mail.python.org/mailman/listinfo/python-list
Sep 5 '06 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Steven Bethard | last post by:
George Sakkis wrote: > "Steven Bethard" <steven.bethard@gmail.com> wrote: >> Dict comprehensions were recently rejected: >> http://www.python.org/peps/pep-0274.html >> The reason, of course,...
3
by: Walter Zydhek | last post by:
I am having a problem using the NameValueCollection type. If I remove one of the items while iterating through an collection of this type, I end up with an exception. This exception is:...
6
by: Johnny Hansen | last post by:
Hello, I've been trying to implement smart pointers in C++ (combined with a reference counter) because I want to do some memory management. My code is based on the gamedev enginuity articles,...
3
by: Jeremy Owens-Boggs | last post by:
We are trying to implement a dual list box selection where you have two list boxes, You highlight items in the right side list box, click a button and this moves those items over to the left hand...
4
by: Gav | last post by:
I am using VS 2005 and am trying to add items to a combo box using C#. I know how to add simple text items but I am trying to add a value and some text ie. Value Text A First Text B ...
9
by: me | last post by:
Hi All, I am new to Classes and learniing the ropes with VB.NET express Here's my question - say I have a want to manage a list of books. Each book has an Author, Title and ISBN Now, I am...
10
by: Backwards | last post by:
Hello all, I'll start by explaining what my app does so not to confuss you when i ask my question. ☺ I have a VB.Net 2.0 app that starts a process (process.start ...) and passes a prameter...
13
by: Eric_Dexter | last post by:
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...
5
by: =?Utf-8?B?U2NhbmJveQ==?= | last post by:
Guyz, I want to remove items from the Solution Explorer. The VBE 2005 help system claims that to do this, you have to:- 1. Select the item you want to remove. 2. On the 'Edit' menu,...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.