Check for new line character?

Discussion in 'Python' started by Yazar Yolait, Oct 26, 2003.

  1. Yazar Yolait

    Yazar Yolait Guest

    I want to skip lines in a file that are blank and that start with "&". So I
    strip(None) them and then startswith("&") but the only problem is if the
    line has nothing but white space and I strip(None) it then it contains
    nothing but a new line character right? So how do I check if the line
    contains a new line character? I can no longer use isspace().
     
    Yazar Yolait, Oct 26, 2003
    #1
    1. Advertising

  2. Yazar Yolait

    John Roth Guest

    "Yazar Yolait" <> wrote in message
    news:bnhk76$v3klv$-berlin.de...
    > I want to skip lines in a file that are blank and that start with "&". So

    I
    > strip(None) them and then startswith("&") but the only problem is if the
    > line has nothing but white space and I strip(None) it then it contains
    > nothing but a new line character right? So how do I check if the line
    > contains a new line character? I can no longer use isspace().


    if line == '\n':
    whatever...

    If you're using <file>.readline() to
    read the lines, then each line (except the eof signal) ***will*** end
    with a newline, and it will not have a newline internally, so there's no
    need for a complicated check. After you strip blanks, an all blank
    line will contain exactly one character: the newline.

    John Roth
    >
    >
     
    John Roth, Oct 26, 2003
    #2
    1. Advertising

  3. Yazar Yolait wrote:

    > I want to skip lines in a file that are blank and that start with "&".
    > So I
    > strip(None) them and then startswith("&") but the only problem is if
    > the
    > line has nothing but white space and I strip(None) it then it contains
    > nothing but a new line character right?


    No, the newline character is whitespace as well so you'll be left with
    the empty string:

    >>> ' \n'.strip()

    ''
    >>> '&\n'.strip()

    '&'

    > So how do I check if the
    > line
    > contains a new line character? I can no longer use isspace().


    Pretty basic:

    while True:
    line = inputFile.readline()
    if not line:
    break
    line = line.strip()
    if not line or line.startswith('&'):
    continue
    ...

    If the rest of your program is sensitive to (non-newline) whitespace,
    then just strip the newline, change

    line = line.strip()

    to

    if line[-1] == '\n':
    line = line[:-1]

    --
    Erik Max Francis && && http://www.alcyone.com/max/
    __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    / \ To endure what is unendurable is true endurance.
    \__/ (a Japanese proverb)
     
    Erik Max Francis, Oct 27, 2003
    #3
  4. John Roth wrote:

    > If you're using <file>.readline() to
    > read the lines, then each line (except the eof signal) ***will*** end
    > with a newline, and it will not have a newline internally, so there's
    > no
    > need for a complicated check.


    Not quite true. It's possible the file will be missing the final
    newline, though Python will still treat it as a separate line:

    max@oxygen:~/tmp% echo newline > test
    max@oxygen:~/tmp% echo -n "no newline" >> test
    max@oxygen:~/tmp% python
    Python 2.3.2 (#1, Oct 3 2003, 15:44:45)
    [GCC 3.2.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> f = file('test')
    >>> f.readline()

    'newline\n'
    >>> f.readline()

    'no newline'
    >>> f.readline()

    ''

    --
    Erik Max Francis && && http://www.alcyone.com/max/
    __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    / \ To endure what is unendurable is true endurance.
    \__/ (a Japanese proverb)
     
    Erik Max Francis, Oct 27, 2003
    #4
    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.

Share This Page