Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
search text file
#1
I have one question with my code if I couldn't find match string it will keep print not find(if your text has 10 line, and could find your match, it will print 10 times). I wish if the string does not match just print one line not found.
Example:
enter your string: sss
not found
not found
not found
not found

i wish to just print one line not find, how can i changed my code


import sys
substring = raw_input("enter your string : ")

		
for line in open('POD.txt'):
    #if ("") in line:
    if substring in line:
        print (line)
	
    else:
        print ("not found")
       
Reply
#2
Have a count variable, and increase it by one each time the line is found. After the loop, print not found if the count is zero.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
i wish to occur like this:
enter your string: sss
not found

do i have to put a if else statement inside the if loop
Reply
#4
You need the if in the loop to check for matches, and add an increment to a count variable to it. You need to take the else out of the loop (unindent it), and convert it to an if count == 0:
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
ok thanks if figure it out thanks.

import sys
substring = raw_input("enter your string : ")

count =0		
for line in open('POD.txt'):
    #if ("") in line:
    if substring in line:
        count +=1
	print (line)
if count==0:
    print ("string not found")
   

But this only support python2, how to let python3 also support.
if i run using python3 it will occur

File "test_file.py", line 10
print (line)
^
TabError: inconsistent use of tabs and spaces in indentation
Reply
#6
You need to make sure your indentation is consistent: either all spaces or all tabs. Try changing all tabs to four spaces or vice versa. Note that line 9 (print(line)) should be indented one more level to be even with count += 1.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Search Excel File with a list of values huzzug 4 1,216 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Search for multiple unknown 3 (2) Byte combinations in a file. lastyle 7 1,318 Aug-14-2023, 02:28 AM
Last Post: deanhystad
  search file by regex SamLiu 1 904 Feb-23-2023, 01:19 PM
Last Post: deanhystad
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,111 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  If function is false search next file mattbatt84 2 1,143 Sep-04-2022, 01:56 PM
Last Post: deanhystad
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,652 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 6,949 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  Search text in PDF and output its page number. atomxkai 21 8,894 Jan-21-2022, 06:20 AM
Last Post: snippsat
  fuzzywuzzy search string in text file marfer 9 4,555 Aug-03-2021, 02:41 AM
Last Post: deanhystad
  Cloning a directory and using a .CSV file as a reference to search and replace bg25lam 2 2,128 May-31-2021, 07:00 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020