Check for new line character?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yazar Yolait

    Check for new line character?

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


  • John Roth

    #2
    Re: Check for new line character?


    "Yazar Yolait" <yazar@hotmail. com> wrote in message
    news:bnhk76$v3k lv$1@ID-198839.news.uni-berlin.de...[color=blue]
    > I want to skip lines in a file that are blank and that start with "&". So[/color]
    I[color=blue]
    > 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().[/color]

    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[color=blue]
    >
    >[/color]


    Comment

    • Erik Max Francis

      #3
      Re: Check for new line character?

      Yazar Yolait wrote:
      [color=blue]
      > 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?[/color]

      No, the newline character is whitespace as well so you'll be left with
      the empty string:
      [color=blue][color=green][color=darkred]
      >>> ' \n'.strip()[/color][/color][/color]
      ''[color=blue][color=green][color=darkred]
      >>> '&\n'.strip()[/color][/color][/color]
      '&'
      [color=blue]
      > So how do I check if the
      > line
      > contains a new line character? I can no longer use isspace().[/color]

      Pretty basic:

      while True:
      line = inputFile.readl ine()
      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 && max@alcyone.com && 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)

      Comment

      • Erik Max Francis

        #4
        Re: Check for new line character?

        John Roth wrote:
        [color=blue]
        > 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.[/color]

        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.[color=blue][color=green][color=darkred]
        >>> f = file('test')
        >>> f.readline()[/color][/color][/color]
        'newline\n'[color=blue][color=green][color=darkred]
        >>> f.readline()[/color][/color][/color]
        'no newline'[color=blue][color=green][color=darkred]
        >>> f.readline()[/color][/color][/color]
        ''

        --
        Erik Max Francis && max@alcyone.com && 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)

        Comment

        Working...