Thank you so much for your guidance, Bruno. This should help me in a long way.
Here is the code I have written. path = raw_input("\nEnter location eg. c:/buffer/test.txt : \n") fileIN = open(path) 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 and data!=""): 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 Thanks Abhi -----Original Message----- From: Bruno Desthuilliers [mailto:bruno.42.desthuilli...@websiteburo.invalid] Sent: Thursday, March 05, 2009 6:50 PM To: python-list@python.org Subject: Re: Help required to read and print lines based on the type of first character (answering to the OP) > En Wed, 04 Mar 2009 07:36:01 -0200, <abhinayaraj.r...@emulex.com> escribió: > >> I am a beginner in Python. In fact, beginner to coding/ scripting. >> >> Here is a scenario, I need to code. Need your help on this: Your first task here should be to refine the specs - too much ambiguities in it: >> A script that >> 1. Reads from a file (may be a local file say test.txt) reads what ? The whole content ? A (yet unspecified) portion ? etc... >> 2. And, if the line which line ? >> begins with a "#", should print the line one >> time "#### yadda" begins with a "#". So according to this rule, it should be printed once. >> 3. if the line has "##", should print the line 2 times, "#### yadda" 'has' (contains) "##", so it should be printed twice. But it also _begins_ with a '#', so according to rule 2, it should be printed once. Is the 'one time' in rule 2 supposed to mean 'at least onces', or 'once and only once' ? In this last case, rule 3 contradicts rule 2. Also, "yadday ## woops" 'has' (contains) "##". According to rule 3, it should be printed twice. Is that right ? >> 4. And, if there are "###" in the beginning of the line, should >> print the same line 3times, "#### yadda" begins with "###", so it should be printed thrice. It also contains "##" (cf rule 3) and starts with "#" (cf rule 2). How is this rule supposed to be understood ? >> 5. And, if the line contains "####" or more "#"'s, then print as >> "an invalid line" "#### yadda" starts with '#', contains '##', and starts with '###'. Which rule is supposed to apply here ? >> 6. if the line contains no "#" then print "looks like a code line" >> The first step in programming is to get accurate, unambigous and well-expressed specs. In the above case (I mean, at this level of detail), "accurate, unambigous and well-expressed specs" are almost pseudocode. Doing a bit of mind-reading (which is certainly *not* the right thing to do - in real life, I'd just go back to the customer or whoever handed me such specs to sort this out), I came out with the following rewrite: 1. open a given file in text mode 2. read it line by line 3. for each line: 3.1. if the line contains/startswith (?) more than three '#': print "an invalid line" 3.2. if the line starts with one, two or three '#': print as many times the line as there are '#' 3.2 else (imply : the line contains no '#'): print "looks like a code line" So basically, you have your algorithm. Now you just need to find out how to do each of these tasks in Python. That is : a. how to open a file for reading in text mode (NB: 'text mode' may or not makes sense, according to the OS) b. how to iterate over the lines in this file once it's correctly opened c. how to test for the presence and position of a given character / substring in a string d. how to "print" something from your program. Good news: all this is pretty well documented. I'd say that the most "tricky" part is c., since there's more than one possible solution, but since it's about strings, looking for what features Python strings has to offer, and trying them out in the interactive interpreter should solve the problem very quickly. HTH -- http://mail.python.org/mailman/listinfo/python-list