Help required to read and print lines based on the type of first character

rdmurray at bitdance.com rdmurray at bitdance.com
Thu Mar 5 08:15:11 EST 2009


Abhinayaraj.Raju at Emulex.Com wrote:
> Thank you for the suggestions.
> 
> Some little reading gave the idea and it works well too. :)
> 
> Here is the code:
> fileIN = open("test.txt")
> count = 0
> for line in fileIN:
> 	data= line
> 
> 	if '####' in data:
> 		count = 4
> 	elif '###' in data:
> 		count = 3
> 	elif '##' in data:
> 		count = 2
> 	elif '#' in data:
> 		count = 1
> 	elif data.find('#') == -1:
> 		count = 0
> 
> 	if (count == 0):
> 		print data + '\nlooks like a code line...\n'
> 	elif(count== 4):
> 		print data + '\ninvalid line!\n'
> 	elif count>=1:
> 		for i in range(0, count):
> 			print data

What happens when your input file has a line like:

    This is a ## fake line

You might want to check out the strip and startswith methods of strings.

--RDM




More information about the Python-list mailing list