Loop through file for words that start with an input value (and count occurrences for each)

Hello, as the title says I’m trying to write a function that will loop through a user input filename to find every word that starts with a user input letter. The file contents and letter are passed through the function as parameters. Then I’m making a dictionary with the key values pairs: {each word: occurrence num}.

The program is a bit large to copy-paste here. So I’ve condensed it to:

def fisearcher():
    filename = input("Enter filename: ")  # using RoadNotTakenRobertFrost.txt
    letter = input("Enter a letter: ")  # assume input == 'f'/'F'

    with open(filename, "r") as f:
        fidata = f.read()
        text = fidata.casefold().split()  # I split contents into csv list to loop over
    for words in text:
        if words.startswith(letter.casefold()):
            n = text.count(words)
            d_wn = {words: n}
            print(f"{words}: {n}")  # outputs full dictionary same as print(d_wn)
# output:
{'far': 1}
{'fair': 2}
{'for': 2}
{'first': 1}
{'for': 2}
{'fair': 2}
{'frost': 1}
  1. I need print these key: value pairs without braces.
    I’ve tried enumerate(d_wn) too, it’s close but it returns prefixed index numbers.
    Tried for k, v in d_wn: print(f"{k}: {v}"}
    Which outputs: error: too many items to unpack
    Tried for k, v in d_wn.items(): print(k, ":", v)
    output same as I included in function
    (Also tried the * operator but there were issues there too that I can’t recall atm.)

  2. Words are being added for each occurrence. If you notice in the output, there are 2 occurrences of ‘for’ and ‘fair’ both in the file and the dictionary. I need the dictionary to include only one key for each word.

# desired output:
'far': 1
'fair': 2
'for': 2
'first': 1
'frost': 1
# apostrophes are fine, just need to remove the curly braces

Can someone help point me in the right direction here? I’ve been stuck on this for the last day smh. Thanks in advance!

d = {k: text.count(k) for k in text if k.startswith(letter.casefold())}
for k, v in d.items():
    print(f"'{k}': {v}")
1 Like

Thank you for your help once again Alexander. You are an international treasure! I tried a version of that, but not quite the same. The puzzle of syntax can be so frustrating when it doesn’t work but so lovely when it does. Your syntax worked perfectly! Though there was a hiccup, not due to your code. After several returns of empty dictionaries, with my old code and your suggested code, I printed the file contents at which point I realized I had saved over the file with ascii art of the solar system. I should get some rest. Thanks again!

1 Like