[Tutor] removing digits from a file

Benoit Dupire bdupire@seatech.fau.edu
Fri, 18 May 2001 08:31:34 -0400


--------------BB01A215290D508C9148A112
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

if there are space between words and numbers, and assuming each number
is folllowed by a word.. you can use string.split()

import string
mystring = "1. that 2. cat 3. rat 4. etc"
number=[]
tokens= string.split(mystring)

for i in (range(0, len(tokens), 2)):
    number.append(int(tokens[i][:-1]))     # the slicing removes the
dot.



Benoit

sheri wrote:

> hello, i am trying to write a script to remove digits from a list of
> spelling words.i have a file like this:1. that 2. cat 3. rat 4.
> etc... i want to do something like this if char (is a digit)   delete
> char so that the output file is like this: that cat rat etc... any
> help would be appreciated. thanks!

--
Benoit Dupire
Graduate Student


--------------BB01A215290D508C9148A112
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<body bgcolor="#FFFFFF">
if there are space between words and numbers, and assuming each number
is folllowed by a word.. you can use string.split()
<p><font face="Arial"><font size=-1>import string</font></font>
<br><font face="Arial"><font size=-1>mystring = "1. that 2. cat 3. rat
4. etc"</font></font>
<br><font face="Arial"><font size=-1>number=[]</font></font>
<br><font face="Arial"><font size=-1>tokens= string.split(mystring)</font></font><font face="Arial"><font size=-1></font></font>
<p><font face="Arial"><font size=-1>for i in (range(0, len(tokens), 2)):</font></font>
<br><font face="Arial"><font size=-1>&nbsp;&nbsp;&nbsp; number.append(int(tokens[i][:-1]))&nbsp;&nbsp;&nbsp;&nbsp;
# the slicing removes the dot.</font></font>
<br><font face="Arial"><font size=-1></font></font>&nbsp;
<br><font face="Arial"><font size=-1></font></font>&nbsp;<font face="Arial"><font size=-1></font></font>
<p><font face="Arial"><font size=-1>Benoit</font></font>
<p>sheri wrote:
<blockquote TYPE=CITE><style></style>
<font face="Arial"><font size=-1>hello,
i am trying to write a script to remove digits from a list of spelling
words.</font></font><font face="Arial"><font size=-1>i have a file like
this:</font></font><font face="Arial"><font size=-1>1. that 2. cat 3. rat
4. etc...</font></font>&nbsp;<font face="Arial"><font size=-1>i want to
do something like this</font></font>&nbsp;<font face="Arial"><font size=-1>if
char (is a digit)</font></font><font face="Arial"><font size=-1>&nbsp;&nbsp;
delete char</font></font>&nbsp;<font face="Arial"><font size=-1>so that
the output file is like this:</font></font><font face="Arial"><font size=-1>&nbsp;that
cat rat etc...</font></font>&nbsp;<font face="Arial"><font size=-1>any
help would be appreciated. thanks!</font></font></blockquote>

<p>--
<br>Benoit Dupire
<br>Graduate Student
<br>&nbsp;
</body>
</html>

--------------BB01A215290D508C9148A112--