6

What I am trying to do is to check whether this string is found in the text file. If it does, I want it to printout that line, else printout a message.

I have implemented this code so far:

 def check_string(string):

     w = raw_input("Input the English word: ")
        if w in open('example.txt').read():
            for w.readlines():
                print line
        else:
            print('The translation cannot be found!')

I've tried implementing that but I got a syntax error.

It says:

invalid syntax at the line -- for w.readlines():

Any idea on how to go with this line of code?

8
  • please post your error message Commented May 8, 2013 at 3:31
  • why does check_string take an argument if it also uses raw_input to get the word that you're looking for? Commented May 8, 2013 at 3:31
  • It says invalid syntax at the line -- for w.readlines(): Commented May 8, 2013 at 3:31
  • 2
    Among other issues your indentation is off. Commented May 8, 2013 at 3:32
  • 1
    have you heard of grep? Commented May 8, 2013 at 4:20

2 Answers 2

8

You should try something like this:

import re
def check_string():
    #no need to pass arguments to function if you're not using them
    w = raw_input("Input the English word: ")

    #open the file using `with` context manager, it'll automatically close the file for you
    with open("example.txt") as f:
        found = False
        for line in f:  #iterate over the file one line at a time(memory efficient)
            if re.search("\b{0}\b".format(w),line):    #if string found is in current line then print it
                print line
                found = True
        if not found:
            print('The translation cannot be found!')

check_string() #now call the function

If you are searching for exact words instead of just sub-string then I would suggest using regex here.

Example:

>>> import re
>>> strs = "foo bar spamm"
>>> "spam" in strs        
True
>>> bool(re.search("\b{0}\b".format("spam"),strs))
False
Sign up to request clarification or add additional context in comments.

4 Comments

Why don't you use re instead? That way, you have more control. Also, the problem with this approach is that it won't match a string that spans multiple lines. Still a good answer though. +1 :)
@AshwiniChaudhary Thanks for the code, I've tried running it, but there seems to be nothing going on. Didn't even prompt me to input. I've put the codefile and the text file in the same directory though.
@user1433571 you have to call the function in order to make it work. try : check_string()
@AshwiniChaudhary Thanks, it works! How do I search for the exact string using regex though? I'm quite new with Python, so still getting the hang of it. Also, what if the string that I'm looking for exist also in another line, how about do I print that out too? Thanks in advance!
4

Here is a bit simpler example by using in operator:

w = raw_input("Input the English word: ") # For Python 3: use input() instead
with open('foo.txt') as f:
    found = False
    for line in f:
        if w in line: # Key line: check if `w` is in the line.
            print(line)
            found = True
    if not found:
        print('The translation cannot be found!')

If you'd like to know the position of the string, then you can use find() instead of in operator.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.