468,301 Members | 1,524 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,301 developers. It's quick & easy.

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

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

Oct 26 '05 #1
6 1600

va**************@yahoo.com 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

Oct 26 '05 #2
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

Oct 26 '05 #3
va**************@yahoo.com 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()

Oct 26 '05 #4
va**************@yahoo.com 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
Oct 26 '05 #5
va**************@yahoo.com 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

Oct 26 '05 #6
Guys - This fixed the issue - thanks to all

Oct 26 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Bryan Feeney | last post: by
6 posts views Thread by Kamus of Kadizhar | last post: by
1 post views Thread by Jamal | last post: by
2 posts views Thread by google.groups.tr | last post: by
1 post views Thread by csharpula csharp | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.