Re: Help required to read and print lines based on the type of firstcharacter

Discussion in 'Python' started by Gabriel Genellina, Mar 4, 2009.

  1. En Wed, 04 Mar 2009 07:36:01 -0200, <> 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
     
    Gabriel Genellina, Mar 4, 2009
    #1
    1. Advertising

  2. (answering to the OP)
    > En Wed, 04 Mar 2009 07:36:01 -0200, <> 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
     
    Bruno Desthuilliers, Mar 5, 2009
    #2
    1. Advertising

  3. Gabriel Genellina

    Guest

    RE: Help required to read and print lines based on the type offirst character

    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:]
    Sent: Thursday, March 05, 2009 6:50 PM
    To:
    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, <> 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
     
    , Mar 5, 2009
    #3
  4. a écrit :

    <ot>
    Please, don't top-post, and learn to quote & snip
    (if you don't know what top-posting is, google is your friend).
    </ot>

    > 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)


    This will break if the file can't be opened (doesn't exist, is
    protected, whatever).

    > count = 0


    'count' is used locally in the loop, so it shouldn't be set outside it.

    > 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:


    This test is redundant. if the "'#' in data" evals to False, then
    data.find('#') is garanteed to return -1.

    > count = 0
    >
    >
    > if (count == 0 and data!=""):


    the file iterator yields lines with the newline character included, so
    there's no way that data == "" (unless you explicitely remove the
    newline character yourself). Read doc for the strip method of string
    objects.

    Also, you don't need the parens.

    > 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


    You didn't address my questions wrt/ specs clarifications. There's a
    *big* difference between *containing* a substring and *starting with* a
    substring. Hint: Python strings have a "startswith" method...

    Also and FWIW, since Python >= 2.5.2 (and possibly >= 2.5.0 - I let you
    check the docs), Python's strings have a count method too.

    Your code is not too bad for a beginner - I've done worse when I started
    -, but it doesn't really matches the specs (which is impossible FWIW
    given the ambiguities they contain, cf my previous post), and contains a
    couple of more or less useless or redundant tests which make it looks a
    bit like "programming by accident", and doesn't really uses Python's
    string handling features.

    HTH
     
    Bruno Desthuilliers, Mar 5, 2009
    #4
  5. Gabriel Genellina

    Guest

    RE: Help required to read and print lines based on the type offirst character

    -----Original Message-----
    From: Bruno Desthuilliers [mailto:]
    Sent: Thursday, March 05, 2009 10:45 PM
    To:
    Subject: Re: Help required to read and print lines based on the type of first character

    a écrit :

    <ot>
    Please, don't top-post, and learn to quote & snip
    (if you don't know what top-posting is, google is your friend).
    </ot>

    > 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)


    This will break if the file can't be opened (doesn't exist, is
    protected, whatever).

    > count = 0


    'count' is used locally in the loop, so it shouldn't be set outside it.

    > 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:


    This test is redundant. if the "'#' in data" evals to False, then
    data.find('#') is garanteed to return -1.

    > count = 0
    >
    >
    > if (count == 0 and data!=""):


    the file iterator yields lines with the newline character included, so
    there's no way that data == "" (unless you explicitely remove the
    newline character yourself). Read doc for the strip method of string
    objects.

    Also, you don't need the parens.

    > 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


    You didn't address my questions wrt/ specs clarifications. There's a
    *big* difference between *containing* a substring and *starting with* a
    substring. Hint: Python strings have a "startswith" method...

    Also and FWIW, since Python >= 2.5.2 (and possibly >= 2.5.0 - I let you
    check the docs), Python's strings have a count method too.

    Your code is not too bad for a beginner - I've done worse when I started
    -, but it doesn't really matches the specs (which is impossible FWIW
    given the ambiguities they contain, cf my previous post), and contains a
    couple of more or less useless or redundant tests which make it looks a
    bit like "programming by accident", and doesn't really uses Python's
    string handling features.

    HTH

    Bruno,
    Thanks once again.

    Yeah, the specification is that it should seek for the first character (sub string) and based on that it should take the decision. So this is "*starting with* a
    substring." Case only.

    And as far as your review comments are concerned, I need to have a look at that all those doc's you have mentioned. That should help.

    -Abhi
     
    , Mar 6, 2009
    #5
  6. a écrit :
    (snip)

    > I need to have a look at that all those doc's you have mentioned. That should help.
    >


    +1 QOTW !-)
     
    Bruno Desthuilliers, Mar 6, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe Wright
    Replies:
    0
    Views:
    554
    Joe Wright
    Jul 27, 2003
  2. Murali
    Replies:
    2
    Views:
    609
    Jerry Coffin
    Mar 9, 2006
  3. keto
    Replies:
    0
    Views:
    1,043
  4. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    399
    David Cournapeau
    Dec 30, 2008
  5. Wolfgang
    Replies:
    1
    Views:
    167
    Paul Lalli
    Feb 13, 2004
Loading...

Share This Page