Search code examples
pythonfilewordsalphabet

Python: Scanning a file for a (3 letter) word


I don't even know how to go about scanning a file (line by line) for a word (with 3 letters) in a logical way, that will let me set a suggested (3 letter) word as a variable (or maybe something better for me to call upon later?).

With that variable, I would first have the program verify with me that it is a 3 letter word.

I am a beginner in python, so I am not very efficient with my coding. I made a python file that is 150+ lines of code that doesn't work. If you would like me to add it just comment and I will add the whole file to this page. I have already looked at the link [Find 3 letter words but it didn't help me with what I am trying to accomplish.


Solution

  • This may help:

    with open("filename.txt") as f:
    lines = f.readlines()
    for line in lines:
        words = [word for word in line.split() if len(word)==3 ]
    print(words)