473,417 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,417 software developers and data experts.

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 1792

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bryan Feeney | last post by:
I'm working on a site which dynamically generates tables of rates in CSV format. The script which does the work is called generate_stats.php. Here's the header header ("Content-type:...
6
by: Kamus of Kadizhar | last post by:
Yet another possible newbie question. I'm tyring to figure out how to best crop a log file. I have a file that grows infinitely in length. I want to keep only the last n entries in the file. ...
4
by: Justin Lebar | last post by:
Sorry about the huge post, but I think this is the amount of information necessary for someone to help me with a good answer. I'm writing a statistical analysis program in ASP.net and MSSQL7 that...
1
by: Jamal | last post by:
I am working on binary files of struct ACTIONS I have a recursive qsort/mergesort hybrid that 1) i'm not a 100% sure works correctly 2) would like to convert to iteration Any comments or...
6
by: Jamal | last post by:
I am working on binary files of struct ACTIONS I have a recursive qsort/mergesort hybrid that 1) i'm not a 100% sure works correctly 2) would like to convert to iteration Any comments or...
2
by: google.groups.tr | last post by:
I have an Access 97 database that has a routine to generate a unique report as a PDF file and email it to one person, and loops about 300 times. Each instance through the loop uses a registry...
1
by: Ryan | last post by:
Hello. I was hoping that someone may be able to assist with an issue that I am experiencing. I have created an Access DB which imports an Excel File with a particular layout and field naming. ...
1
by: csharpula csharp | last post by:
Hello, I need to generate cs files from XSD files. I would like to do it automatically. What is the best way to do it? Is it better be done with PreBuild inside VS (before compilation) or to...
2
by: totalnewbie | last post by:
Hi all, Here's the situation. We are sending out a survey and tracking who we send it to. The automated system that sends the survey generates a text file called "surveydd mmm yyyy.txt" every...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.