Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Output file formatting/loop problems -- HELP?

Reply
Thread Tools

Output file formatting/loop problems -- HELP?

 
 
Maggie
Guest
Posts: n/a
 
      09-08-2009
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
 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      09-08-2009
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


 
Reply With Quote
 
 
 
 
Maggie
Guest
Posts: n/a
 
      09-08-2009
On Sep 8, 11:39*am, MRAB <(E-Mail Removed)> 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?


 
Reply With Quote
 
Hendrik van Rooyen
Guest
Posts: n/a
 
      09-08-2009
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
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      09-08-2009
Maggie wrote:
> On Sep 8, 11:39 am, MRAB <(E-Mail Removed)> 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.
 
Reply With Quote
 
Maggie
Guest
Posts: n/a
 
      09-08-2009
On Sep 8, 12:35*pm, MRAB <(E-Mail Removed)> wrote:
> Maggie wrote:
> > On Sep 8, 11:39 am, MRAB <(E-Mail Removed)> 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!!!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problems with writing to an output file Ameya Perl Misc 5 05-21-2009 04:24 PM
Newbie: How do I filter output to the screen and writing the orginal output to a file? Mav Perl Misc 22 07-09-2004 09:56 PM
parse output screen ok but cant get desired output new file! chuck amadi Python 1 06-23-2004 02:16 PM
Is Fuji S3000 3.2m/pixel output, or 6 m/pixel interpolated output? Peter H Digital Photography 43 12-04-2003 02:35 PM
Output / Debug window output bug? John Bentley ASP .Net 0 09-10-2003 07:38 AM



Advertisments