Output file formatting/loop problems -- HELP?

M

Maggie

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
 
M

MRAB

Maggie said:
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.
 
M

Maggie

Try adding:
      print repr(item)

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

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?
 
H

Hendrik van Rooyen

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
 
M

MRAB

Maggie said:
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.
 
M

Maggie

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!!!
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top