dimanche 27 mars 2016

counting the times a word that starts with an A or a appears from a file

Ok so I have this program and its supposed to read a file, "englishsorted.txt", and count the number of times a word that begins with an "A" or "a" occurs. I'm supposed to use a pattern object. I am not really sure what I'm missing/have wrong. I appreciate the help.

import sys
import re

count = 0
x = open("englishsorted.txt", "r")

while 1:
    pattern = x.readline()
    if pattern == re.compile(r'^[A,a].'): #regex that finds words starting with an A or a
    count = count + 1

x.close()

print count

Aucun commentaire:

Enregistrer un commentaire