Results 1 to 5 of 5

Thread: Problem comparing strings

  1. #1
    Join Date
    Mar 2005
    Location
    Isle of Bute, Scotland
    Beans
    369
    Distro
    Ubuntu Gnome 14.04 Trusty Tahr

    Problem comparing strings

    I am trying to make up a word search type of program as a way to learn more python.
    Ultimately I will want user input for adding new words to the list so that may be different again.
    I am doing it in stages and this is my program so far:
    Code:
    #!/usr/bin/python
    from time import sleep
    myword = 'john'
    infile = open('/home/norman/dict/WORDS.TXT', 'r')
    for myline in infile:
        print myline
        sleep(1)
    
    
    if str(myline).lower() == str(myword).lower() :
    #if myline == myword :
    #if myline.strip() is myword.strip() :
        print('Found')
        sleep(5)
    
    
    infile.close()
    This is the contents of WORDS.TXT which I typed into gedit.
    fred
    harry
    tom
    lilly
    anne
    john
    christine
    neil
    None of the options I have tried works so I am at a loss as to the correct syntax to get the result I want.
    Please show me what will work.
    thanks
    Running 14.04 on my HP Pavilion g6 4 Gig Ram 320 Gig Disc

  2. #2
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Problem comparing strings

    maybe use a list?

    Code:
    >>> with open('words.txt','r') as f:
    ...     wordlist = [w for w in f.read().split()]
    ... 
    >>> wordlist
    ['fred', 'harry', 'tom', 'lilly', 'anne', 'john', 'christine', 'neil']
    >>> 
    >>> 
    >>> if 'john' in wordlist:
    ...     print 'found'
    ... else:
    ...     print 'not found'
    ... 
    found
    >>> 
    >>> if 'bob' in wordlist:
    ...     print 'found'
    ... else:
    ...     print 'not found'
    ... 
    not found
    >>>

  3. #3
    Join Date
    Mar 2005
    Location
    Isle of Bute, Scotland
    Beans
    369
    Distro
    Ubuntu Gnome 14.04 Trusty Tahr

    Re: Problem comparing strings

    Great thanks, I altered it to run non-interactively:
    Code:
    #!/usr/bin/python
    myword = 'tom'
    f = open('./words.txt','r') 
    wordlist = f.read().split()
    wordlist
    ['fred', 'harry', 'tom', 'lilly', 'anne', 'john', 'christine', 'neil']
     
    if myword in wordlist:
         print 'found'
    and it works fine so on to the next stage.
    Running 14.04 on my HP Pavilion g6 4 Gig Ram 320 Gig Disc

  4. #4
    Join Date
    May 2008
    Location
    UK
    Beans
    1,451
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Problem comparing strings

    There are a number of reasons why your initial code would not have worked :
    1) When reading text from a file - each line ends with a \n character - which you need to remove.
    2) Assuming the indentation was correct - your if statement occurred after the end of your loop - so you were only comparing agsint the last line
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  5. #5
    Join Date
    Mar 2005
    Location
    Isle of Bute, Scotland
    Beans
    369
    Distro
    Ubuntu Gnome 14.04 Trusty Tahr

    Re: Problem comparing strings

    Quote Originally Posted by Tony Flury View Post
    There are a number of reasons why your initial code would not have worked :
    1) When reading text from a file - each line ends with a \n character - which you need to remove.
    2) Assuming the indentation was correct - your if statement occurred after the end of your loop - so you were only comparing agsint the last line
    Ah yes! What a dummy I am not to have spotted that!
    I went back to the original code and altered it to this:
    Code:
    #!/usr/bin/python
    from time import sleep
    myword = 'john'
    infile = open('/home/norman/dict/CROSSWDX.TXT', 'r')
    for myline in infile:
        print myline
        sleep(1)
        if str(myline.strip()).lower() == str(myword).lower():
            print('Found')
            sleep(5)
    
    
    infile.close()
    which does indeed work.

    Thanks guys 2 ways to do it.
    Not sure which will ultimately work best as I develop this but it's great to have both to try.
    Running 14.04 on my HP Pavilion g6 4 Gig Ram 320 Gig Disc

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •