How to find out if specific word is found in file

This is the place for queries that don't fit in any of the other categories.

How to find out if specific word is found in file

Postby zvonko92 » Mon Jul 28, 2014 4:17 pm

I have file called "dictionary.txt" with a lot of words with grammatical label, for example:

house/af

world/df

computer/gd

The letters after the character "/" represent some grammatical label. My task is to make a code in Python that will check whether a specific word is in the file. For example, if you define word "computer", the program should confirm that the word is in file "dictionary.txt".
Last edited by micseydel on Mon Jul 28, 2014 4:37 pm, edited 1 time in total.
Reason: First post lock.
zvonko92
 
Posts: 8
Joined: Mon Jul 28, 2014 4:13 pm

Re: How to find out if specific word is found in file

Postby micseydel » Mon Jul 28, 2014 4:39 pm

You'll probably want to read this if you want good answers to get you going quickly. Make especially sure to read the part about
Code: Select all
code tags
.

From what it sounds like, you'll just want to open the file, iterate over the lines, and store the left side of the line in a set.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1390
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: How to find out if specific word is found in file

Postby zvonko92 » Tue Jul 29, 2014 11:13 am

Hello,
Thanks for answer.

I have tried with this code but it does not work for some reason.It prints "False" on every string input. Could you help me find out why?

Code: Select all
f=open("file1.txt",'r')
f0=open("file2.txt",'w')
f1=open("file2.txt",'r')

for line in f:
   s=line.split('/')
   f0.write("%s"%s)

a=raw_input('write some string')


def check():
       
        found = False
        for line in f1:
            if ("%s"%a) in line:
      found=True
   print found
               
               
check()
zvonko92
 
Posts: 8
Joined: Mon Jul 28, 2014 4:13 pm

Re: How to find out if specific word is found in file

Postby micseydel » Tue Jul 29, 2014 4:13 pm

There are several issues here. You typically don't want to read and write the same file at the same time. You can use "r+" instead of "r" or "w" I think (double check the docs) but I don't think you need to do that here. Also
Code: Select all
for line in f:
   s=line.split('/')
   f0.write("%s"%s)

will just give you a file with a bunch of Python lists dumped into the file as strings. I think you want something more like
Code: Select all
user_word = raw_input("Write some string: ")
with open("file1.txt") as lines:
    for line in lines:
        if line.split('/')[0] == word:
            print "Found it!"
            break
    else:
        print "Didn't find it."

Note that I used the rarely-used else clause of a for-loop which gets executed if and only if the loop exits normally, without a break (or return or whatever else). This version of the code "short circuits" in the sense that it won't go through the whole file if it doesn't need to. If the else clause confuses you, this could be re-written with a "flag" variable (found) as you did above.

I also want to point out (constructively) that although the function you had was legal Python, it's not how functions are typically used. In your function, you access a presumed global variable, a, and that in the long run that kind of code will be really difficult to read, write and maintain (and get help with!). Instead, you'll want to use function parameters and have your function not access any variables outside of its own scope unless they're constants, including other functions, modules, and even constant values you define yourself.

If anything here wasn't clear, please do ask questions. I see some things in your code that indicate strongly you're trying to learn, but have somehow picked up some bad habits that will work against you at some point.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1390
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: How to find out if specific word is found in file

Postby zvonko92 » Tue Jul 29, 2014 7:37 pm

Thanks very much for help. I also appreciate your comment about my bad programming habits. I will change it.
zvonko92
 
Posts: 8
Joined: Mon Jul 28, 2014 4:13 pm

Re: How to find out if specific word is found in file

Postby zvonko92 » Wed Jul 30, 2014 7:52 am

I have another problem. This time it is about alfabet. Since I use Croatian language, I use a few charachters (č,ć,ž,đ,š) which are not recognized by code written above. For example, if I write string "čokolada", program says "Didn't find it", althought word "čokolada" is found in file. How to solve this?
zvonko92
 
Posts: 8
Joined: Mon Jul 28, 2014 4:13 pm

Re: How to find out if specific word is found in file

Postby Kebap » Thu Jul 31, 2014 9:04 am

zvonko92 wrote:I use a few charachters (č,ć,ž,đ,š) which are not recognized by code written above.

Hello zvonko, do you already know about character encoding? This is done for special characters, which are not standard ascii. However, it can be done in different ways. If the file is saved in one way, and you search another way, chances are the characters will not be found.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 396
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: How to find out if specific word is found in file

Postby zvonko92 » Fri Aug 01, 2014 10:49 am

I understand what you said, but I need exact steps to make this code work for Croatian alfabet.
zvonko92
 
Posts: 8
Joined: Mon Jul 28, 2014 4:13 pm

Re: How to find out if specific word is found in file

Postby snippsat » Fri Aug 01, 2014 11:59 am

I understand what you said, but I need exact steps to make this code work for Croatian alfabet.

add u(unicode) or convert to utf-8(unicode)
Code: Select all
# Python 2.7
>>> s = 'čćžđš'
>>> s
'\xc4\x8d\xc4\x87\xc5\xbe\xc4\x91\xc5\xa1'
>>> print s
čćžđš # Garbages
>>> print type(s)
<type 'str'>

>>> s = s.decode('utf-8')
>>> s
u'\u010d\u0107\u017e\u0111\u0161'
>>> print s
čćžđš
>>> print type(s)
<type 'unicode'>

Add u
Code: Select all
# Python 2.7
>>> s = u'čćžđš'
>>> s
u'\u010d\u0107\u017e\u0111\u0161'
>>> print s
čćžđš
>>> type(s)
<type 'unicode'>

Read in file with utf-8.
Code: Select all
# Python 2.7
import codecs

with codecs.open("file1.txt", 'r', encoding='utf-8') as f:
    print f.read() #--> čćžđš
User avatar
snippsat
 
Posts: 231
Joined: Thu Feb 21, 2013 12:04 am


Return to General Coding Help

Who is online

Users browsing this forum: Baidu [Spider] and 4 guests