[Tutor] removing digits from a file
Roeland Rengelink
r.b.rigilink@chello.nl
Fri, 18 May 2001 15:08:28 +0200
> 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!
Hi Sheri,
I'm going to assume two things.
- you're using Python2.0 or later
- in your file is each line is layed-out as follows
number whitespace word whitespace number ...
If either one is not true, this will not work
The tricky thing in the following code is the function that tests if
a word is a number. Feel free to ask for further explanation.
def isnumber(word):
'''A function that returns 1 (true) if word is a number, else 0
(false)
A number is defined a something that can be converted to a float'''
try:
a = float(word)
return 1 # got here, so float(word) didn't raise an error
except ValueError:
return 0 # float(word) raised an error
infile = open('input_filename', 'r')
outfile = open('output_filename', 'w')
while 1:
line = infile.readline()
if not line:
#reached end of file
break
word_list = line.split()
out_list = []
for word in word_list:
if not isnumber(word):
#it's not a number, so it must be a real word
out_list.append(word)
# make a line from the words in out_list and write it
out_line = ' '.join(out_list)+'\n'
outfile.write(out_line)
Hope this helps,
Roeland
_
r.b.rigilink@chello.nl
"Half of what I say is nonsense. Unfortunately I don't know which half"