Typing tutor help script needed, please

SPE - Stani's Python Editor spe.stani.be at gmail.com
Fri Oct 28 14:33:34 EDT 2005


#---Input data

#List of words; every word or sentence on one line
#If from file: WORDS = open(fileName).readlines()
WORDS = """\
Afrikaans
Anna
Bread
red
word
bored
python""".split('\n')

#---Main program
import re

PATTERN         = ['[^(%s)]+','[%s]+']
FILENAME        = ['not_%s.txt','%s.txt']

def filter(letters='bdeor',words=WORDS,contain=True,ignoreCase=True):
    pattern     = PATTERN[contain]%'|'.join(list(letters))
    if ignoreCase:
        allowed = re.compile(pattern,re.IGNORECASE)
    else:
        allowed = re.compile(pattern)
    result      = []
    for word in words:
        match   = allowed.match(word)
        if match and match.group(0) == word: result.append(word)
    print result
    output      = open(FILENAME[contain]%letters,'w')
    output.write('\n'.join(result))
    output.close()



if __name__ == '__main__':
    filter()

---------------
This should do it.

Stani
--
http://pythonide.stani.be
http://pythonide.stani.be/manual/html/manual.html




More information about the Python-list mailing list