×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How can I repeat my search?

How can I repeat my search?

How can I repeat my search?

(OP)
I think I have this doing what I want except I would like it to search each line throughout the document adding the amount of times the group was found.

I was thinking I could do this with a while loop and readline() although I could not make it work and if I put a readline() in place of the read() I couldn't get the results I was with read() it would come up "was not found" I am not sure why because my search was on the first line in the document.

If anyone could let me know what I am doing wrong or if there is a better way that would be grate. Thanks.

This is the code I am using now without repeating over the lines.

CODE

look_in = raw_input ("Enter the search file to look in ")
search = raw_input ("Enter your search item ")

txt = open(look_in).read()
search2 = search.split()

for item in search2:
    c = txt.count(item)
if c > 0: print "(",c,")", "Of your search was found"
else: print item, "was not found"

 

RE: How can I repeat my search?

Hi

Quote (chuckiechan1):

I was thinking I could do this with a while loop and readline() although I could not make it work and if I put a readline() in place of the read() I couldn't get the results I was with read() it would come up "was not found"
If you have problem with the readline() version, then why not posted that one ?

Anyway. If you want to read & process line-by-line, then
  • you will need a dict to store the intermediary results
  • better user a for loop

CODE --> Python

look_in = raw_input ("Enter the search file to look in ")
search = raw_input ("Enter your search item ")

fil = open(look_in)
search2 = search.split()
c = {}
for item in search2:
  c[item] = 0

for txt in fil:
  for item in search2:
    c[item] += txt.count(item)

fil.close()

for item in search2:
  if c[item] > 0: print "(",c[item],")", "Of your search was found"
  else: print item, "was not found"

Feherke.
http://free.rootshell.be/~feherke/

RE: How can I repeat my search?

(OP)
ok I will try it with a for loop thanks for the help

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close