473,587 Members | 2,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(SUBHEAD ER + ": " + 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 1800

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(SUBHEAD ER + ": " + 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(SUBHEAD ER + ": " + 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(SUBHEAD ER + ": " + 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(SUBHEAD ER + ": " + 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(HEAD ER + "\n")
f.write(SUBHEA DER + ": " + 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(SUBHEAD ER + ": " + 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
2744
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: application/octet-stream"); header ("Content-Disposition: attachment; filename=query-results.csv"); header ("Pragma: no-cache"); header ("Expires: 0"); Now...
6
2719
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. I've been looking for some sort of ascii-database or log file management python module that lets me say: how many records in the file? and then...
4
3012
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 analyzes data that I've collected from my business's webpage and the hits it's collecting from the various pay-per-click (PPC) engines. I've...
1
3375
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 suggestion for improvements or conversion to iteration would be much appreciated
6
2866
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 suggestion for improvements or conversion to iteration would be much appreciated
2
1633
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 entry to pre-populate the desired filename for the PDF file it generates. The PDFs are generated, attached and emailed in the loop as they're...
1
5310
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. Next the user can go into a Form which basically a dynamic query with a friendly interface that eventually outputs a table that is stored in the DB as...
1
1954
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 activate this action outside of VS in extrenal script (and not on each compilation)? Thank you! *** Sent via Developersdex...
2
1751
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 time a survey is sent (with the date being the date the survey is sent). I have created a macro in Access that imports this kind of text file and I...
0
7915
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7967
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6619
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
2347
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.