On 10/23/2009 05:16 AM, Dave Angel wrote: > Steve wrote: >> Sorry I'm not being clear >> >> ****Input****** >> sold: 16 >> sold: 20 >> sold: 2 >> sold: 0 >> sold: <storefront> >> 7 >> 0 >> <storefront> >> sold >> null >> >> ****Output**** >> 16 >> 20 >> 2 >> 0 >> 0 >> 7 >> 0 >> 0 >> 0 >> 0 > > Since you're looking for only digits, simply make a string containing > all characters that aren't digits. > > Now, loop through the file and use str.translate() to delete all the > non-digits from each line, using the above table. > > Check if the result is "", and if so, substitute "0". Done.
delchars = ''.join([chr(i) for i in range (256) if chr(i)<'0' or chr(i) >'9']) f = open ('your_input_filename.txt') for line in f: num = line.translate (None, delchars) if num == '': num = '0' print num IMO, Python's translate function in unpleasant enough to use that I might use a regex here, although the code above is likely faster if you are running this on large input files. import re f = open ("your_input_filename.txt") for line in f: mo = re.search ('\d+', line) if mo: print mo.group(0) else: print '0' -- http://mail.python.org/mailman/listinfo/python-list