Output file formatting/loop problems -- HELP?

Discussion in 'Python' started by Maggie, Sep 8, 2009.

  1. Maggie

    Maggie Guest

    My code is supposed to enumerate each line of file (1, 2, 3...) and
    write the new version into the output file --

    #!/usr/bin/python

    import os.path
    import csv
    import sys

    #name of output file
    filename = "OUTPUT.txt"


    #open the file
    test = open ("test.txt", "r")

    #read in all the data into a list
    readData = test.readlines()

    count = 0

    FILE = open(filename, "w")

    for item in readData:

    count = count + 1
    tmp_string = str(count) + ' ' + item
    print >> FILE, tmp_string

    else:
    print 'The loop is finito'

    ---

    here is the sample file --

    23
    123
    231
    1231

    ---

    the output file i get looks like this:

    1 23
    123
    231
    1231

    --

    my question is why the enumeration starts and stops at first line and
    doesnt go through the entire file --

    (file is saved as .txt, so hypothetically no .rtf formatting that
    would screw up the output should be present)

    thanks for your help
    Maggie, Sep 8, 2009
    #1
    1. Advertising

  2. Maggie

    MRAB Guest

    Maggie wrote:
    > My code is supposed to enumerate each line of file (1, 2, 3...) and
    > write the new version into the output file --
    >
    > #!/usr/bin/python
    >
    > import os.path
    > import csv
    > import sys
    >
    > #name of output file
    > filename = "OUTPUT.txt"
    >
    >
    > #open the file
    > test = open ("test.txt", "r")
    >
    > #read in all the data into a list
    > readData = test.readlines()
    >
    > count = 0
    >
    > FILE = open(filename, "w")
    >
    > for item in readData:
    >

    Try adding:
    print repr(item)

    here to see what the lines actually look like. It might be a problem
    with line endings.

    > count = count + 1
    > tmp_string = str(count) + ' ' + item
    > print >> FILE, tmp_string
    >
    > else:
    > print 'The loop is finito'
    >
    > ---
    >
    > here is the sample file --
    >
    > 23
    > 123
    > 231
    > 1231
    >
    > ---
    >
    > the output file i get looks like this:
    >
    > 1 23
    > 123
    > 231
    > 1231
    >
    > --
    >
    > my question is why the enumeration starts and stops at first line and
    > doesnt go through the entire file --
    >
    > (file is saved as .txt, so hypothetically no .rtf formatting that
    > would screw up the output should be present)
    >
    > thanks for your help
    MRAB, Sep 8, 2009
    #2
    1. Advertising

  3. Maggie

    Maggie Guest

    On Sep 8, 11:39 am, MRAB <> wrote:
    > Maggie wrote:
    > > My code is supposed to enumerate each line of file (1, 2, 3...) and
    > > write the new version into the output file --

    >
    > > #!/usr/bin/python

    >
    > > import os.path
    > > import csv
    > > import sys

    >
    > > #name of output file
    > > filename = "OUTPUT.txt"

    >
    > > #open the file
    > > test = open ("test.txt", "r")

    >
    > > #read in all the data into a list
    > > readData = test.readlines()

    >
    > > count = 0

    >
    > > FILE = open(filename, "w")

    >
    > > for item in readData:

    >
    > Try adding:
    >       print repr(item)
    >
    > here to see what the lines actually look like. It might be a problem
    > with line endings.
    >
    > >    count = count + 1
    > >    tmp_string = str(count) + '     ' + item
    > >    print >> FILE, tmp_string

    >
    > > else:
    > >    print 'The loop is finito'

    >
    > > ---

    >
    > > here is the sample file --

    >
    > > 23
    > > 123
    > > 231
    > > 1231

    >
    > > ---

    >
    > > the output file i get looks like this:

    >
    > > 1  23
    > > 123
    > > 231
    > > 1231

    >
    > > --

    >
    > > my question is why the enumeration starts and stops at first line and
    > > doesnt go through the entire file --

    >
    > > (file is saved as .txt, so hypothetically no .rtf formatting that
    > > would screw up the output should be present)

    >
    > > thanks for your help

    >
    >


    great tip, thanks so much -- now this is the output i get in the
    terminal...

    '23\r123\r231\r1231'

    why is it so? since the file is in .txt format - there should be no
    formatting involved?... how would i fix this?
    Maggie, Sep 8, 2009
    #3
  4. On Tuesday 08 September 2009 17:22:30 Maggie wrote:
    > My code is supposed to enumerate each line of file (1, 2, 3...) and
    > write the new version into the output file --
    >
    > #!/usr/bin/python
    >
    > import os.path
    > import csv
    > import sys
    >
    > #name of output file
    > filename = "OUTPUT.txt"
    >
    >
    > #open the file
    > test = open ("test.txt", "r")


    After this, do the following and see what you get:

    for i,line in enumerate(test.readlines()):
    print i, line

    > my question is why the enumeration starts and stops at first line and
    > doesnt go through the entire file --


    It does - but it sees the entire file as one line, somehow.

    > (file is saved as .txt, so hypothetically no .rtf formatting that
    > would screw up the output should be present)


    If it is really text, and if there are newlines at the end of the lines, then
    it should JustWork...

    - Hendrik
    Hendrik van Rooyen, Sep 8, 2009
    #4
  5. Maggie

    MRAB Guest

    Maggie wrote:
    > On Sep 8, 11:39 am, MRAB <> wrote:
    >> Maggie wrote:
    >>> My code is supposed to enumerate each line of file (1, 2, 3...) and
    >>> write the new version into the output file --
    >>> #!/usr/bin/python
    >>> import os.path
    >>> import csv
    >>> import sys
    >>> #name of output file
    >>> filename = "OUTPUT.txt"
    >>> #open the file
    >>> test = open ("test.txt", "r")
    >>> #read in all the data into a list
    >>> readData = test.readlines()
    >>> count = 0
    >>> FILE = open(filename, "w")
    >>> for item in readData:

    >> Try adding:
    >> print repr(item)
    >>
    >> here to see what the lines actually look like. It might be a problem
    >> with line endings.
    >>
    >>> count = count + 1
    >>> tmp_string = str(count) + ' ' + item
    >>> print >> FILE, tmp_string
    >>> else:
    >>> print 'The loop is finito'
    >>> ---
    >>> here is the sample file --
    >>> 23
    >>> 123
    >>> 231
    >>> 1231
    >>> ---
    >>> the output file i get looks like this:
    >>> 1 23
    >>> 123
    >>> 231
    >>> 1231
    >>> --
    >>> my question is why the enumeration starts and stops at first line and
    >>> doesnt go through the entire file --
    >>> (file is saved as .txt, so hypothetically no .rtf formatting that
    >>> would screw up the output should be present)
    >>> thanks for your help

    >>

    >
    > great tip, thanks so much -- now this is the output i get in the
    > terminal...
    >
    > '23\r123\r231\r1231'
    >
    > why is it so? since the file is in .txt format - there should be no
    > formatting involved?... how would i fix this?
    >

    It shows that the line endings are carriage returns '\r'.

    Line endings on Windows are '\r\n', on Unix/Linux are '\n' and on MacOS
    are '\r', although recent versions of MacOS built on top of Unix.

    The easiest solution would be to open the file in universal line-ending
    mode:

    test = open ("test.txt", "rU")

    This will translate any of the line endings.
    MRAB, Sep 8, 2009
    #5
  6. Maggie

    Maggie Guest

    On Sep 8, 12:35 pm, MRAB <> wrote:
    > Maggie wrote:
    > > On Sep 8, 11:39 am, MRAB <> wrote:
    > >> Maggie wrote:
    > >>> My code is supposed to enumerate each line of file (1, 2, 3...) and
    > >>> write the new version into the output file --
    > >>> #!/usr/bin/python
    > >>> import os.path
    > >>> import csv
    > >>> import sys
    > >>> #name of output file
    > >>> filename = "OUTPUT.txt"
    > >>> #open the file
    > >>> test = open ("test.txt", "r")
    > >>> #read in all the data into a list
    > >>> readData = test.readlines()
    > >>> count = 0
    > >>> FILE = open(filename, "w")
    > >>> for item in readData:
    > >> Try adding:
    > >>       print repr(item)

    >
    > >> here to see what the lines actually look like. It might be a problem
    > >> with line endings.

    >
    > >>>    count = count + 1
    > >>>    tmp_string = str(count) + '     ' + item
    > >>>    print >> FILE, tmp_string
    > >>> else:
    > >>>    print 'The loop is finito'
    > >>> ---
    > >>> here is the sample file --
    > >>> 23
    > >>> 123
    > >>> 231
    > >>> 1231
    > >>> ---
    > >>> the output file i get looks like this:
    > >>> 1  23
    > >>> 123
    > >>> 231
    > >>> 1231
    > >>> --
    > >>> my question is why the enumeration starts and stops at first line and
    > >>> doesnt go through the entire file --
    > >>> (file is saved as .txt, so hypothetically no .rtf formatting that
    > >>> would screw up the output should be present)
    > >>> thanks for your help

    >
    > > great tip, thanks so much -- now this is the output i get in the
    > > terminal...

    >
    > > '23\r123\r231\r1231'

    >
    > > why is it so? since the file is in .txt format - there should be no
    > > formatting involved?... how would i fix this?

    >
    > It shows that the line endings are carriage returns '\r'.
    >
    > Line endings on Windows are '\r\n', on Unix/Linux are '\n' and on MacOS
    > are '\r', although recent versions of MacOS built on top of Unix.
    >
    > The easiest solution would be to open the file in universal line-ending
    > mode:
    >
    >      test = open ("test.txt", "rU")
    >
    > This will translate any of the line endings.


    works beautifully now! thank you all for your input!!!
    Maggie, Sep 8, 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. mark
    Replies:
    1
    Views:
    270
    Victor Bazarov
    Jun 15, 2004
  2. Replies:
    1
    Views:
    288
    Grizlyk
    Jan 15, 2007
  3. addi
    Replies:
    0
    Views:
    263
  4. Mothra
    Replies:
    4
    Views:
    179
    Mothra
    Dec 9, 2003
  5. Isaac Won
    Replies:
    9
    Views:
    354
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page