Search code examples
pythonpython-3.2

Goes to a file and prints only the words that don't have any digits in them


Also I need to make the program show the words that DO have digits in thee digits in them SEPERATELY

f = open("lolpa.txt", "r")

list1 = (f)
temp = []

for item in list1:
    if "1" "2" "3" "4" "5" "6" "7" "8" "9" "0"  in item:
        temp.append(item)
    else:
        print(item)

is what i have so far, but it for some reason shows all the words.

EDIT: The lolpa.txt is just a file for comparison

EDIT: If that would change anything , I'm using python 3.2.


Solution

  • Something like this will get you started, question isn't very clear.

    with open("lolpa.txt") as f:
        for word in f.readline().split(): # assuming all words are on the first line
            digits = [c for c in word if c.isdigit()]
            if digits: # digits list is not empty
                print(' '.join(digits)) # shows digits with space in between
            else:
                print(word) # prints word normally