Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

find and re.search in python

# This is how we use find
# Open a file
hand = open('path and the name of the file')
count1 = 0
for line in hand:
    # remove new line (/n) at the end of the line
    line = line.rstrip()
    # If there is no 'From' it will give a -1
    if line.find('Enter the sub string here')>= 0:
        count1 += 1
        print(count1, line)
# Close the file
hand.close()

# This is how we use re.search()
import re
count2 = 0
hand2 = open('path and the name of the file')
for line2 in hand2:
    line2 = line2.rstrip()
    if re.search('Enter the sub string here', line2):
        count2 += 1
        print(count2, line2)
# Close the file
hand2.close()
Source by www.py4e.com #
 
PREVIOUS NEXT
Tagged: #find #python
ADD COMMENT
Topic
Name
6+9 =