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

V

vasilijepetkovic

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
 
I

Iain King

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
 
J

johan.appelgren

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
 
J

John Abel

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
 
M

Marcus Ekelund

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()
 
P

Peter Otten

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
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top