I have defined a function that searches a string for all 5-letter words starting with the letter I’ve specified. A text file is then created with the processed list.
I currently must run the program manually for each letter. How can I change my code so that the function will search for all letters of the alphabet one after the other and make a separate text file for each result?
import re
from pathlib import Path
# define starting letter of word search
ltr = 'm'
# define RegEx pattern to find all 5-letter words starting with a
certain letter
pattern = r'\b'+ltr+r'\w{4}\b'
# define Function WordSearch: search wordlist.txt with defined RegEx
Pattern
def WordSearch():
# set Path to Main Text file
file = r'C:\Users\User1\OneDrive\Documents\wordlist.txt'
# open Main Text file and set variable 'MainText' equal to contents
fileToOpen = Path(file)
f = open(fileToOpen)
MainText = f.read()
# search all words in Main Text and make list of those that fit the
# RegEx pattern defined above
slist = re.findall(pattern,MainText)
# make set from list to eliminate duplicate entries
slist_nd = list(set(slist))
# turn Set into a string
sliststr = ' '.join(slist_nd)
# open new txt file, write string into contents
slist_file = open(r"C:\Users\User1\OneDrive\Documents\fivelist"+ltr+".txt", "w")
slist_file.write(sliststr)
# close files
f.close()
slist_file.close()
WordSearch()
>Solution :
Here you go.
import string
letters = string.ascii_lowercase
Then you can do
for ltr in letters:
pattern = r'\b'+ltr+r'\w{4}\b'
Note that you have to indent all forward code to be in the for loop.