I need to find a way to print "Cannot find birthday" if the "searchline" is not found in the "pidigits" file. Maybe using a count somehow. I've tried a lot of different things, but I cannot figure it out. I have attached both files.

import string

def find():
	blength = len(open("birthday.txt").readlines())
	length = len(open("pidigits.txt").readlines())
	inBirthday = open("birthday.txt", "r")
	search = 0
	for line in range(blength):
		
		inPiDigits = open("pidigits.txt", "r")
		if search < 0:
			print "Cannot Find Birthday."
			
		
		searchline = inBirthday.readline()
		L1 = inPiDigits.readline()
		
		for lines in range(length):
			L2 = inPiDigits.readline()
			linePiDigits = L1[:50]+L2[:-46]
			search = linePiDigits.find(searchline[:6])
			L1 = L2
			count +=1
			if search >=0:
				
				print "Searching for:",searchline,"line",lines+1,"character position",search
				print 
			
		inPiDigits.close		
find()

I figured out this problem, but i need the output to look like this :
Searching for: 022348
line 16554 character position 13
line 17533 character position 37
for searchlines that show up more than once in the pidigits file.
Anyone know how I can do this?

import string

def find():
	blength = len(open("birthday.txt").readlines())
	length = len(open("pidigits.txt").readlines())
	inBirthday = open("birthday.txt", "r")
	count = 1
	for line in range(blength+1):
		
		inPiDigits = open("pidigits.txt", "r")
		if count < 1:
			print "Searching for:", searchline,"Birthday not found!\n"
		else:
			count = 0
		
		count = 0
		searchline = inBirthday.readline()
		L1 = inPiDigits.readline()
		
		for lines in range(length):
			L2 = inPiDigits.readline()
			linePiDigits = L1[:50]+L2[:-46]
			search = linePiDigits.find(searchline[:6])
			L1 = L2
			if search >0:
				count +=1
				print "Searching for:",searchline,"line",lines+1,"character position",search
				print
			elif count == 0:
				count = 0
				notfoundline = searchline
		inPiDigits.close		
find()

OK, I started working on this when I saw your original post. But since I started, you made your second post. So I've modified it a little to give the line numbers; but I've not got it to output the character position yet!

I've done the search using the re module (regular expressions).

Here's the code I've got so far...It only works if all lines in birthday.txt end with a newline! Otherwise the last line gets a character removed by my code!

Anyway, this is what I've got so far: (not quite the same as yours but it kinda does the job!)

import string
import re

def find():
    # read both text files in their entirety...
    piDigits = open("pidigits.txt").readlines()
    birthdays = open("birthday.txt").readlines()

    # iterate through the dates
    for date in birthdays:
        wasFound=False

        # build a search string
        # need to strip out the newlines...
        # ensure each line in birthday.txt ends with a newline
        # otherwise the last birthday will have the last digit removed!
        searchString=""
        for idx in range(0, len(date)-1):
            searchString+=date[idx]

        # as long as we have exactly 6 chars in the search string
        # we can continue!
        if len(searchString)==6:
            print "\n\nSearching for", searchString, ":"
            # iterate through the piDigits and use the re module
            # (Regular expressions) to see if the birthday is found
            count=0
            for line in piDigits:
                count+=1
                result = re.search(searchString, line)
                if result: # if found, set flag and output.
                    wasFound=True
                    print "The birthday", searchString, "was found at line ", count


            # If not found at all output a message    
            if not wasFound:
                print "The birthday", searchString, "was not found!\n"

find()

Cheers for now,
Jas.

I just started using reg expressions myself, but this should help you figure out how to get the position into your output. It will have to be modified a bit, but here is how I have been doing it. Hope it helps.

import re

string = "5672035890"

look = re.compile(r"2035")

x = look.sub("x", string)
x = x.find("x")
print(x)

My final code.

import string

def find():
	outFile = open("results.txt", "w")
	
	# find the length of the input files
	bLength = len(open("birthday.txt").readlines())
	length = len(open("pidigits.txt").readlines())
	
	# open the file
	inBirthday = open("birthday.txt", "r")
	# start a count so you can print the birthdays not found
	count = 1
	
	# start the outer loop to go through the birthdays
	for line in range(bLength+1):
		# open the pi file everytime through the loop
		inPiDigits = open("pidigits.txt", "r")
		
		# print out not found if the count is 0
		if count < 1:
			outFile.write("Birthday not found!\n\n")
		else:
			count = 0
		
		# read a line from the birthday file
		searchLine = inBirthday.readline()
		# start to read the line
		L1 = inPiDigits.readline()
		# index the searchline to take out the newline character
		cutSearchLine = searchLine[:6]
		
		# make an if statement to print out the birthday searched for (if statement is -
		# used to keep the program from printing "searching for" an extra time)
		if line < bLength:
			outFile.write ("\n")
			outFile.write ("Searching for: %s \n" %(cutSearchLine))
		
		# start a for loop to go through each line 
		for lines in range(length):
			
			# read the next line in the pi file
			L2 = inPiDigits.readline()
			# combine the first line and the first 5 digits of the next line
			linePiDigits = L1[:50]+L2[:-46]
			
			# use the find function to return the position of the first character
			# if the birthday is found in the pi file.
			search = linePiDigits.find(cutSearchLine)
			#swap the 2nd line with the first in order to keep going through the file
			L1 = L2
			if search >0:
				# increment count if the birthday is found.
				count +=1
				# output the line number and position if found.
				outFile.write ("line %s character position %s \n" %(lines+1, search))
			elif count == 0:
				count = 0
				
				
		inPiDigits.close		
find()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.