473,749 Members | 2,350 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Search txt file for user input.

3 New Member
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 2136
boxfish
469 Recognized Expert Contributor
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
myself2211
3 New Member
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 Recognized Expert Contributor
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
myself2211
3 New Member
Thank you boxfish.
:)
Nov 13 '08 #5

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

Similar topics

1
8726
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, returned the hyperlinked page title, filename, and the body txt (30 preceding and following words) in context with the search word highlighted. Excellent.! See it working at: http://www.ipt.co.za Just search for "firearm"
8
3221
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 'search' may be clicked on by the user and the user can then search all records by postcode. I want to do this to prevent duplicate data entry.
5
3609
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? Robert This is what I need: I need to be able to search on a text field in a table. I want to give the user the opportunity to search on
4
9019
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, even the removal, I just don't know how to keep track of the parent, so that I can set its child to the child of the node to be removed. IE - if I had C / \ B D
4
2115
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 can do that, could you please post the relevant files. I have searched the internet for a while now, and have been completely unsuccessful. I would like to do it using javascript in my XSL file if possible, or using CGI script to search the XML...
3
1944
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. My index page has two fields: customer password Here are the contents of both of my files. I'm sure I am missing something here but 4 hours later and I am stll in the same place.
1
1493
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" method="post" action="index.php"> <input name="Search_IN" type="text" id="Search_IN" maxlength="44" /> <input type="submit" name="SearchBTN" id="SearchBTN" value="Search" />
1
7549
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 having the sql statement dynamically built according to the input provided by the user. I have used the method described here hundreds of times it is quick and adaptive. I generally use a frames page for the search, in this way the search is maintained...
0
1126
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 search engines which will automatically index the user input from the XML file(s), so I can create a search functionality based on keywords. I am aware that a DB solution is the obvious route. The purpose here
0
9388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8256
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.