I wrote a utility to scan a text file for all space delimited fields that contain alpha characters, it works great but is very slow because I am splitting every line into words and scanning each word, is there a faster way to do this?
Thanks.
Here is the code:
#!/bin/python
import argparse
import sys
import time
parser = argparse.ArgumentParser(description='Find all alpha characters in
an input file')
parser.add_argument('file', type=argparse.FileType('r'),
help='filename.txt')
args = parser.parse_args()
def letters(input):
output = []
for character in input:
if character.isalpha():
output = input
return output
def main(argv):
start = time.time()
fname = sys.argv[1]
f = open(fname)
for line in f:
words = line.rstrip().split()
for word in words:
alphaWord = letters(word)
if alphaWord:
print(alphaWord)
f.close()
end = time.time()
elapsed = end - start
print "%s secs elapsed" % elapsed
if __name__ == "__main__":
main(sys.argv)