-4

I have a file called 'dictionary.txt' which contains words of the dictionary in alphabetical order.

I want to search if certain words are in this dictionary. Could anyone help me write code that could open this file, then I write a word as an input and I receive an output that says if the word is in the dictionary file or not?

Here's what I have so far:-

dicfile = open('dictionary.txt', 'r') 
word = input('enter a word: ') 
if word in dicfile: 
    print('true') 
else: 
    print('false')
the.salman.a
  • 945
  • 8
  • 29
Hamza Noor
  • 21
  • 1
  • 1
  • nice explanation can you show your code? – Narendra Apr 07 '18 at 06:20
  • dicfile = open('dictionary.txt', 'r') word = input('enter a word: ') if word in dicfile: print('true') else: print('false') – Hamza Noor Apr 07 '18 at 06:21
  • I am still a beginner at python so any help would be much appreciated. – Hamza Noor Apr 07 '18 at 06:23
  • Possible duplicate of [Reading a text file and splitting it into single words in python](https://stackoverflow.com/questions/16922214/reading-a-text-file-and-splitting-it-into-single-words-in-python) – zwer Apr 07 '18 at 06:27

6 Answers6

2
fd = open("abc.txt","r")    # open the file in read mode
file_contents = fd.read()   # read file contents
word = "hello"              # input word to be searched
if(word in file_contents):  # check if word is present or not
    print("word found")
else:
    print("word not found")
fd.close()                  # close the file
Mufeed
  • 3,018
  • 4
  • 20
  • 29
2

You were almost there, just needed a little more info about file handling in python.

The dictionary.txt file:

bac def ghij kl mano pqrstuv

Here's the your modified code code

dicfile = open('dictionary.txt', 'r') 
file1 = dicfile.read()
file1 = file1.split()

word = input('enter a word: ') 
if word in file1:
    print('true')
else:
    print('false')

The Output:

Test Case 1

$ python3 test_9.py

enter a word: bac
true

Test Case 2

$ python3 test_9.py

enter a word: anyword
false
the.salman.a
  • 945
  • 8
  • 29
1
 def check_word(filename, word):
     with open(filename) as fin:
         if word in fin.read():
              return True
         else:
             return False
Abhishake Gupta
  • 2,939
  • 1
  • 25
  • 34
0

Suppose you want to find x in the file. You can do like this :

with open("textfile.txt") as openfile:
    for line in openfile:
        for part in line.split():
            if "x" in part:
                print part
            else:
                print false 

`

ParthS007
  • 2,581
  • 1
  • 22
  • 37
0

This is one method using procedural programming

dictionary = open('dictionary.txt', 'r')  #Only read

word = input('Enter a word:\n)  #input typed on line below this

if word in dictionary:
    print('{} is in the dictionary (True)'.format(word))
else:
    print('{} is not in the dictionary (False)'.format(word))

Here's another one:

def in_file(filename, word):
    file = open(filename, 'r')
    if word in file:
        return True
    else:
        return False

word = input("Enter a word:\n")
in_file('dictionary.txt', word)

For non-case sensitive results you could use word.lower(). Hope this helps.

Ethanol
  • 370
  • 6
  • 18
0

Stack Overflow is not code writing service, We try to help people only when they try to do something and stuck , I am not giving an exact solution but I will show you how you can achieve your goal, and rest is your homework:

Good answer by @Srikar Appalaraju

#Sample 1 - elucidating each step but not memory efficient
lines = []
with open("C:\name\MyDocuments\numbers") as file:
    for line in file: 
        line = line.strip() #or some other preprocessing
        lines.append(line) #storing everything in memory!

#Sample 2 - a more pythonic and idiomatic way but still not memory efficient
with open("C:\name\MyDocuments\numbers") as file:
    lines = [line.strip() for line in file]

#Sample 3 - a more pythonic way with efficient memory usage. Proper usage of with and file iterators. 
with open("C:\name\MyDocuments\numbers") as file:
    for line in file:
        line = line.strip() #preprocess line
        doSomethingWithThisLine(line) #take action on line instead of storing in a list. more memory efficient at the cost of execution speed.

Example :

with open('dictionary.txt','r') as f:
    #for reading line by line
    for line in f:

        print(line)





    #reading whole file once
    print(f.read())

Now how to check word in file :

Using python 'in' operator

#checking word
if 'some_word' in line:
    print(True)
else:
    print(False)

Now try something and when you stuck then ask help, instead of asking to write code.

Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88