473,570 Members | 2,989 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2694
Sounds like you need to use html parser, check it out in the
documentation.. ..

<Er*********@ms n.comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.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*********@ms n.comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.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(li ne)
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.splitex t() 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(orcfilenam e, '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(orcfilenam e, '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*********@ms n.com>
Newsgroups: comp.lang.pytho n
To: <py*********@py thon.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*********@ms n.com>
Newsgroups: comp.lang.pytho n
To: <py*********@py thon.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:

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

<CsoundSynthesi zer>;
; 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>

<CsInstrument s>
; 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>

</CsoundSynthesiz er>

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

If I understand correctly you want to write the instruments block to a file (from <CsInstrumentst o </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*********@ms n.com>
Newsgroups: comp.lang.pytho n
To: <py*********@py thon.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*********@ms n.com>
Newsgroups: comp.lang.pytho n
To: <py*********@py thon.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:

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

<CsoundSynthesi zer>;
; 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>

<CsInstrument s>
; 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>

</CsoundSynthesiz er>

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

If I understand correctly you want to write the instruments block to a file (from <CsInstrumentst o </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*********@ms n.com>
Newsgroups: comp.lang.pytho n
To: <py*********@py thon.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*********@ms n.com>
Newsgroups: comp.lang.pytho n
To: <py*********@py thon.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

<CsInstrument s>
; 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:

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

<CsoundSynthesi zer>;
; 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>

<CsInstrument s>
; 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>

</CsoundSynthesiz er>

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

If I understand correctly you want to write the instruments block to a file (from <CsInstrumentst o </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*********@ms n.com>
Newsgroups: comp.lang.pytho n
To: <py*********@py thon.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*********@ms n.com>
Newsgroups: comp.lang.pytho n
To: <py*********@py thon.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

<CsInstrument s>
; 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 BeautifulStoneS oup

soup = BeautifulStoneS oup(s)
csin = soup.contents[0].contents[5]
lines = csin.string.spl itlines()

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.spl itlines()" 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

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

Similar topics

30
3449
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, is that dict comprehensions don't gain you >> much at all over the dict() constructor plus a generator expression, >> e.g.: >> dict((i,...
3
9342
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: Collection was modified after the enumerator was instantiated. This happens when it attempts to continue through the enumeration.
6
1795
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, various books and ... whatever I could find on the subject :-) I'll leave out the reference counter part because its pretty basic, but my reference...
3
3109
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 list box. The problem is that if there are many items selected (thousands), then removing the items from the right side list box takes for ever...
4
24747
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 Second Text I've seen examples of ListItem but I can't seem to get this to work, can anybody offer any advice.
9
2015
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 used to using Arrays so I would normally do something like this: Set an array up during the init routine (called from form_load) say of
10
6767
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 through from a combo box. The combo box items are made up of IP address and computer host name. Anything a user places in this combo box it writes...
13
1708
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 not be at the end of the line starting with ; until the end of the line including white spaces.. this is a corrected version from ...
5
1521
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, choose 'Remove'. The problem is that there is no 'Edit' menu in the Solution Explorer, and the
0
7638
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7956
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8152
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6336
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5523
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3684
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3671
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1238
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
974
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.