I prepared a small script that uses re.finditerto find all integers (you can change the regular expression so that it can deal with floats or scientific notation) and then use mapto return a list of scaled numbers.
import re
def scale(fact):
"""This function returns a lambda which will scale a number by a
factor 'fact'"""
return lambda val: fact * val
def find_and_scale(file, fact):
"""This function will find all the numbers (integers) in a file and
return a list of all such numbers scaled by a factor 'fact'"""
num = re.compile('(\d+)')
scaling = scale(fact)
f = open(file, 'r').read()
numbers = [int(m.group(1)) for m in num.finditer(f)]
return map(scaling, numbers)
if __name__ == "__main__":
import sys
if len(sys.argv) != 3:
print "usage: %s file factor" % sys.argv[0]
sys.exit(-1)
numbers = find_and_scale(sys.argv[1], int(sys.argv[2]))
for number in numbers:
print "%d " % number
file, fact, script python script.py file fact, STDOUT . , - , ...