Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Looping Problem (Generating files - only the last record generates a file)

Reply
Thread Tools

Looping Problem (Generating files - only the last record generates a file)

 
 
vasilijepetkovic@yahoo.com
Guest
Posts: n/a
 
      10-26-2005
Hello All,

I have a problem with the program that should generate x number of txt
files (x is the number of records in the file datafile.txt).

Once I execute the program (see below) only one file (instead of x
files) is created. The file created is based on the last record in
datafile.txt.

The program is as follows:
====================================
#! python

HEADER = "This page displays longitude-latitude information"
SUBHEADER = "City"

for line in open("datafile.txt"):


town, latlong = line.split('\t')

f = open(town + ".txt", "w+")

f.write(HEADER + "\n")
f.write(SUBHEADER + ": " + town + "\n")
f.write("LAT/LONG" + ": " + latlong + "\n")
f.close()


# end
====================================




The datafile.txt is as follows (tab separated columns):
====================================

NYC 1111-2222
Lima 3333-4444
Rome 5555-6666

====================================

Once executed, the program will create a single file (named Rome.txt)
and it would not create files NYC.txt and Lima.txt as I would expect it
to do.

I'd appreciate if you can pinpoint my error.

Best,

Vasa

 
Reply With Quote
 
 
 
 
Iain King
Guest
Posts: n/a
 
      10-26-2005

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hello All,
>
> I have a problem with the program that should generate x number of txt
> files (x is the number of records in the file datafile.txt).
>
> Once I execute the program (see below) only one file (instead of x
> files) is created. The file created is based on the last record in
> datafile.txt.
>
> The program is as follows:
> ====================================
> #! python
>
> HEADER = "This page displays longitude-latitude information"
> SUBHEADER = "City"
>
> for line in open("datafile.txt"):
>
>
> town, latlong = line.split('\t')
>
> f = open(town + ".txt", "w+")
>
> f.write(HEADER + "\n")
> f.write(SUBHEADER + ": " + town + "\n")
> f.write("LAT/LONG" + ": " + latlong + "\n")
> f.close()
>
>
> # end
> ====================================
>
>
>
>
> The datafile.txt is as follows (tab separated columns):
> ====================================
>
> NYC 1111-2222
> Lima 3333-4444
> Rome 5555-6666
>
> ====================================
>
> Once executed, the program will create a single file (named Rome.txt)
> and it would not create files NYC.txt and Lima.txt as I would expect it
> to do.
>
> I'd appreciate if you can pinpoint my error.
>
> Best,
>
> Vasa


Did you try indenting the last five lines?

Iain

 
Reply With Quote
 
 
 
 
johan.appelgren@gmail.com
Guest
Posts: n/a
 
      10-26-2005
You have only indented the first line in the for-loop, so for each line
in the file you split the line into town and latlong. Then after you
have split the last line in the file you write a new file with the last
result in the for-loop.

What you want is probably something like this:

#! python

HEADER = "This page displays longitude-latitude information"
SUBHEADER = "City"

for line in open("datafile.txt"):
town, latlong = line.split('\t')
f = open(town + ".txt", "w+")
f.write(HEADER + "\n")
f.write(SUBHEADER + ": " + town + "\n")
f.write("LAT/LONG" + ": " + latlong + "\n")
f.close()

# end

/Johan

 
Reply With Quote
 
John Abel
Guest
Posts: n/a
 
      10-26-2005
(E-Mail Removed) wrote:

>Hello All,
>
>I have a problem with the program that should generate x number of txt
>files (x is the number of records in the file datafile.txt).
>
>Once I execute the program (see below) only one file (instead of x
>files) is created. The file created is based on the last record in
>datafile.txt.
>
>The program is as follows:
>====================================
>#! python
>
>HEADER = "This page displays longitude-latitude information"
>SUBHEADER = "City"
>
>for line in open("datafile.txt"):
>
>
> town, latlong = line.split('\t')
>
>f = open(town + ".txt", "w+")
>
>f.write(HEADER + "\n")
>f.write(SUBHEADER + ": " + town + "\n")
>f.write("LAT/LONG" + ": " + latlong + "\n")
>f.close()
>
>
>

These lines need to be within your loop.

J
 
Reply With Quote
 
Marcus Ekelund
Guest
Posts: n/a
 
      10-26-2005
(E-Mail Removed) wrote:
> Hello All,
>
> I have a problem with the program that should generate x number of txt
> files (x is the number of records in the file datafile.txt).
>
> Once I execute the program (see below) only one file (instead of x
> files) is created. The file created is based on the last record in
> datafile.txt.
>
> The program is as follows:
> ====================================
> #! python
>
> HEADER = "This page displays longitude-latitude information"
> SUBHEADER = "City"
>
> for line in open("datafile.txt"):
>
>
> town, latlong = line.split('\t')
>
> f = open(town + ".txt", "w+")
>
> f.write(HEADER + "\n")
> f.write(SUBHEADER + ": " + town + "\n")
> f.write("LAT/LONG" + ": " + latlong + "\n")
> f.close()
>
>
> # end
> ====================================
>
>
>
>
> The datafile.txt is as follows (tab separated columns):
> ====================================
>
> NYC 1111-2222
> Lima 3333-4444
> Rome 5555-6666
>
> ====================================
>
> Once executed, the program will create a single file (named Rome.txt)
> and it would not create files NYC.txt and Lima.txt as I would expect it
> to do.
>
> I'd appreciate if you can pinpoint my error.


Since the lines that handle writing to the file aren't indented as far
as the line that splits the data, they are not part of the loop. They
are only executed once after the loop has completed, with town and
latlong set to the values they got at the last iteration of the loop.

It should look more like this:

for line in open("datafile.txt"):
town, latlong = line.split('\t')

f = open(town + ".txt", "w+")
f.write(HEADER + "\n")
f.write(SUBHEADER + ": " + town + "\n")
f.write("LAT/LONG" + ": " + latlong + "\n")
f.close()

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      10-26-2005
(E-Mail Removed) wrote:

> Once I execute the program (see below) only one file (instead of x
> files) is created. The file created is based on the last record in
> datafile.txt.


> #! python
>
> HEADER = "This page displays longitude-latitude information"
> SUBHEADER = "City"
>
> for line in open("datafile.txt"):
>
>
> town, latlong = line.split('\t')


In Python whitespace is significant. For the following to be executed in the
for-loop it has to be indented to the same level as the line above.

> f = open(town + ".txt", "w+")
>
> f.write(HEADER + "\n")
> f.write(SUBHEADER + ": " + town + "\n")
> f.write("LAT/LONG" + ": " + latlong + "\n")
> f.close()
>
>
> # end
> ====================================


The effect of aligning it with the for-statement is that it is executed only
once /after/ the loop has run to completion. At that point town and latlong
are still bound to the values they were assigned in the last iteration
(with an empty datafile.txt the loop would never be executed and you would
get a NameError).

Peter

 
Reply With Quote
 
vasilijepetkovic@yahoo.com
Guest
Posts: n/a
 
      10-26-2005
Guys - This fixed the issue - thanks to all

 
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
looping in array vs looping in a dic giuseppe.amatulli@gmail.com Python 5 09-20-2012 11:58 PM
How to replace the last (and only last) character in a string? Johny Python 8 05-03-2007 09:24 PM
Repeater. I see only the last record. Have no idea why! shapper ASP .Net 3 02-27-2007 02:29 AM
how to delete last node of a Linked list if you only know the address of last node. sangram C Programming 16 12-01-2006 08:57 PM
get last record from database without looping Matt ASP General 1 01-10-2004 08:18 AM



Advertisments