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

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Mar 4 12:36:10 CET 2009


En Wed, 04 Mar 2009 07:36:01 -0200, <Abhinayaraj.Raju at 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:
>
> A script that
> 1.      Reads from a file (may be a local file say test.txt)
> 2.      And, if the line begins with a "#", should print the line one  
> time
> 3.      if the line has "##", should print the line 2 times,
> 4.      And, if there are "###" in the beginning of the line, should  
> print the same line 3times,
> 5.      And, if the line contains "####" or more "#"'s, then print as  
> "an invalid line"
> 6.      if the line contains no "#" then print "looks like a code line"
>
> For this as far the info I could understand,
>
> -       We need to use open('TextFile.txt') function
> -       Need to start a While loop/ For in loop
> -       By some means individually read every single line and check for  
> the above conditions

Start following the tutorial at http://docs.python.org/tut

You'll see it menctions a few ways to process a file; the cleanest  
alternative to do it one line at a time is:
	for line in some_file:
	    # do something with line

The interactive interpreter is great to experiment:

py> x = "some text"
py> x.startswith("#")
False
py> y = "## another text"
py> y.startswith("#")
True

-- 
Gabriel Genellina




More information about the Python-list mailing list