473,383 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Search txt file for user input.

Hi, this is my first attempt at Python, what I am trying to achieve is a script that will ask the user for input, search a txt file and display the output or give not found message. Ideally I would like it to ask the user once the first output is achieved for further user input if required, otherwise exit the program. However, would be happy if it achieved the first objective.
Following is as far as I can go, the problem is it is breaking after the first find, whereabouts I know there are additional matches in the txt file. I think I might need to replace the IF with a WHILE, which I have tried but keep getting errors, and am unable to work it out.

Expand|Select|Wrap|Line Numbers
  1. print"\nSEARCHING NUMBERS"  
  2.  
  3. print "\nPlease Enter the Number 1, 2, 3, 4 or 5."  
  4.  
  5. text_file = open("read_it.txt", "r")  
  6.  
  7. word = raw_input("Type the Number you want to check: ") word = word.upper()  
  8.  
  9. print "\nnumber." 
  10. for lines in text_file:  
  11.  
  12. # if the line does not contain the typed number 
  13. # then continue to the next line      
  14.  
  15. if ''.join(lines).find(word) == -1:continue         
  16. print lines 
  17.  
  18. break 
  19. else:        
  20.  
  21. print "Not found"     # Executed whenever break above is NOT executed 
  22. print  raw_input("\n\nPress the enter key to exit.")  
  23.  
  24. text_file.close()  
I am only a beginner, so would appreciate any help or suggestions thank you.
L :)
Nov 12 '08 #1
4 2113
boxfish
469 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1. # if the line does not contain the typed number
  2. # then continue to the next line
  3. if ''.join(lines).find(word) == -1:continue
  4. print lines
  5. break
  6. else:
  7.     print "Not found"     # Executed whenever break above is NOT executed
  8.  
This code is confused. I don't know which code is supposed to be in the if block. A continue jumps directly up to the top of the loop. There's no point in doing anything like breaking or printing anything after you have used continue. For now, you don't have to use continue or break; a simple if-else should do it.
Expand|Select|Wrap|Line Numbers
  1. if the word is found:
  2.     print "Found"
  3. else:
  4.     print "Not found"
  5.  
As for checking if the word is found, the find function is not nessecary. just use the in operator:
Expand|Select|Wrap|Line Numbers
  1. if word in lines:
  2.     print "Found"
  3.  
Hope this helps.
Nov 12 '08 #2
thanks Boxfish, I am continuously getting an "invalid syntax"
Expand|Select|Wrap|Line Numbers
  1. if the 'word' is found:
.

I appreciate your feedback and can see the confusion, (it's a true reflection of the writer).

L :(
Nov 12 '08 #3
boxfish
469 Expert 256MB
Sorry, that was pseudocode. Use the second bit of code that uses the in operator to find the word.
The more you program, the less confused you will be.
Nov 12 '08 #4
Thank you boxfish.
:)
Nov 13 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Les Juby | last post by:
A year or two back I needed a search script to scan thru HTML files on a client site. Usual sorta thing. A quick search turned up a neat script that provided great search results. It was fast,...
8
by: Steph | last post by:
Hi. I'm very new to MS Access and have been presented with an Access database of contacts by my employer. I am trying to redesign the main form of the database so that a button entitled...
5
by: Robert | last post by:
Does anybody have the code for, an example of, or a link to a text-search function that supports exact phrase, any word, and all words searching in an Access table. If so, could you please post it?...
4
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working,...
4
by: zakhirn | last post by:
Hello, I have an extremely new user to XML and XSL, and I would like to know how to search XML data via a input form in HTML, and have the results displayed in HTML. If anyone has code that...
3
by: fjm | last post by:
Hello all, I have 2 files. index.php which is a log in page and a search page. After the user logs in they are taken to the search page by way of a checklogin script that verifies the user. ...
1
by: smartic | last post by:
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Search</title> </head> <body> <form id="form1" name="form1"...
1
Merlin1857
by: Merlin1857 | last post by:
How to search multiple fields using ASP A major issue for me when I first started writing in VB Script was constructing the ability to search a table using multiple field input from a form and...
0
by: phl | last post by:
hello, I am trying to explore options on the various types of search engines for searching user input. User input maybe stored in XML files. Does anyone know if there are any good ready made...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.