Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I repeat my search?

Status
Not open for further replies.

chuckiechan1

Programmer
Feb 5, 2010
2
CA
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"

 
Hi

chuckiechan1 said:
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 [tt]readline()[/tt] version, then why not posted that one ?

Anyway. If you want to read & process line-by-line, then
[ul]
[li]you will need a [tt]dict[/tt] to store the intermediary results[/li]
[li]better user a [tt]for[/tt] loop[/li]
[/ul]
Python:
look_in [teal]=[/teal] [COLOR=darkgoldenrod]raw_input[/color] [teal]([/teal][green][i]"Enter the search file to look in "[/i][/green][teal])[/teal]
search [teal]=[/teal] [COLOR=darkgoldenrod]raw_input[/color] [teal]([/teal][green][i]"Enter your search item "[/i][/green][teal])[/teal]

fil [teal]=[/teal] [COLOR=darkgoldenrod]open[/color][teal]([/teal]look_in[teal])[/teal]
search2 [teal]=[/teal] search[teal].[/teal][COLOR=darkgoldenrod]split[/color][teal]()[/teal]
c [teal]=[/teal] [teal]{}[/teal]
[b]for[/b] item [b]in[/b] search2[teal]:[/teal]
  c[teal][[/teal]item[teal]][/teal] [teal]=[/teal] [purple]0[/purple]

[b]for[/b] txt [b]in[/b] fil[teal]:[/teal]
  [b]for[/b] item [b]in[/b] search2[teal]:[/teal]
    c[teal][[/teal]item[teal]][/teal] [teal]+=[/teal] txt[teal].[/teal][COLOR=darkgoldenrod]count[/color][teal]([/teal]item[teal])[/teal]

fil[teal].[/teal][COLOR=darkgoldenrod]close[/color][teal]()[/teal]

[b]for[/b] item [b]in[/b] search2[teal]:[/teal]
  [b]if[/b] c[teal][[/teal]item[teal]][/teal] [teal]>[/teal] [purple]0[/purple][teal]:[/teal] [b]print[/b] [green][i]"("[/i][/green][teal],[/teal]c[teal][[/teal]item[teal]],[/teal][green][i]")"[/i][/green][teal],[/teal] [green][i]"Of your search was found"[/i][/green]
  [b]else[/b][teal]:[/teal] [b]print[/b] item[teal],[/teal] [green][i]"was not found"[/i][/green]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top