What is the best way to handle a missing newline in the following case

Discussion in 'Python' started by chad, Nov 5, 2010.

  1. chad

    chad Guest

    I have an text file with the following numbers


    Now the program reads in this file. When it encounters a '\n', it will
    do some stuff and then move to the next line. For example, it will
    read 1 and then '\n'. When it sees '\n', it will do some stuff and go
    on to read 3.

    The problem is when I get to the last line. When the program sees '\n'
    after the 9, everything works fine. However, when there isn't a '\n',
    the program doesn't process the last line.

    What would be the best approach to handle the case of the possible
    missing '\n' at the end of the file?
    chad, Nov 5, 2010
    1. Advertisements

  2. chad

    Peter Otten Guest

    Don't split the data into lines yourself, delegate to python
    .... f.write("1\n2\n3\r\n4\r5")
    ........ print repr(line)

    As you can see "\n", "\r\n" and "\r" are all converted to "\n". This is
    called universal newline mode and enabled by open(..., "U"). If your client
    code insists that a line has to end with "\n" and there's no way to change
    that you can wrap the file
    .... prev = next(instream)
    .... for cur in instream:
    .... yield prev
    .... prev = cur
    .... if not prev.endswith("\n"):
    .... prev += "\n"
    .... yield prev
    ........ print repr(line)

    But often the opposite direction, removing any newlines, works just as well
    and is easier to achieve:
    .... print repr(line.strip("\n"))

    Peter Otten, Nov 5, 2010
    1. Advertisements

  3. chad

    danmcleran Guest

    The problem is when I get to the last line. When the program sees '\n'
    use readines to read all lines into a list and then iterate thru the

    f = open(r'c:\test.txt', 'rb')
    print f.readlines()
    ['1\r\n', '3\r\n', '5\r\n', '7\r\n', '3\r\n', '9']
    danmcleran, Nov 5, 2010
  4. chad

    Paul Rudin Guest

    There's no real point in contructing a list. Just do

    with open(r'c:\test.txt') as f:
    for l in f:
    print int(l)

    As long as you just have digits and whitespace then that's fine - int()
    will do as you want.
    Paul Rudin, Nov 5, 2010
  5. chad

    Neil Cerutti Guest

    Keep in mind that a file that a text file that doesn't end with
    a newline isn't strictly legal. You can expect problems, or at
    least warnings, with other tools with such files.
    Neil Cerutti, Nov 5, 2010
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.