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

Discussion in 'Python' started by vasilijepetkovic@yahoo.com, Oct 26, 2005.

  1. Guest

    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, 2005
    #1
    1. Advertising

  2. Iain King Guest

    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
    Iain King, Oct 26, 2005
    #2
    1. Advertising

  3. Guest

    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, 2005
    #3
  4. John Abel Guest

    Re: Looping Problem (Generating files - only the last record generatesa file)

    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
    John Abel, Oct 26, 2005
    #4
  5. 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()
    Marcus Ekelund, Oct 26, 2005
    #5
  6. Peter Otten Guest

    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
    Peter Otten, Oct 26, 2005
    #6
  7. Guest

    Guys - This fixed the issue - thanks to all
    , Oct 26, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shapper
    Replies:
    3
    Views:
    409
    shapper
    Feb 27, 2007
  2. sangram
    Replies:
    16
    Views:
    1,966
  3. Johny
    Replies:
    8
    Views:
    385
  4. Matt
    Replies:
    1
    Views:
    159
    Aaron Bertrand [MVP]
    Jan 10, 2004
  5. Replies:
    5
    Views:
    263
Loading...

Share This Page