Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to run word search function for each letter of the alphabet

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()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading