Output file formatting/loop problems -- HELP?

Maggie la.foma at gmail.com
Tue Sep 8 11:44:36 EDT 2009


On Sep 8, 11:39 am, MRAB <pyt... at mrabarnett.plus.com> 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?





More information about the Python-list mailing list