Eric1989
Eric1989

Reputation: 639

Python - Writing from a file to two files

I wish to have a function that takes in a input file and outputs 2 files. The first output file I have done but the second output file will depend on the results of the first which I cannot get working...

Input file:

FirstName,LastName,DOB,POSTCODE
FirstName,LastName,DOB,POSTCODE
FirstName,LastName,DOB,POSTCODE

Output file 1:

Firstname,DOB
Firstname,DOB
Firstname,DOB

Outputfile 2: (ONLY IF DOB equals 2000)

Firstname,POSTCODE
Firstname,POSTCODE
Firstname,POSTCODE

I have functions that do this task perfectly well but now I wish to further the function for my learning benefits and have become stuck...

What I want to happen...

IF the DOB in output file 1 is '2000', then write to output file 3.

Current function:

def func(iFile):
    with open(iFile) as iF:
        with open("DOB.txt", "a") as oF1:
            for line in iF:
                chars = line.split(',')
                oF1.write(chars[0] + ',' + chars[2])
                oF1.write('\n')
        with open("DOB.txt", "a") as iF1:
            with open("POSTCODE.txt", "a") as oF2:
                chars = line.split(',')
                for line in iF:
                    if char[2] == "2000":
                        oF2.write(chars[0] + ',' + chars[3])
                        oF2.write('\n')

So based on this....

I want the input file into the function first split into one file with the firstname & DOB, then for each line within that file, if the DOB of that file is '2000', i want that output in a new firstname.POSTCODE file.

My current script creates both files. The DOB.txt file is populated but makes duplicate lines. The POSTCODE.txt file is empty.

Any help will be appreciated. :)

Upvotes: 0

Views: 1151

Answers (4)

nouseforname
nouseforname

Reputation: 748

So you have this input:

FirstName,LastName,DOB,POSTCODE
FirstName,LastName,2000,POSTCODE
FirstName,LastName,DOB,POSTCODE
FirstName,LastName,DOB,POSTCODE
FirstName,LastName,2000,POSTCODE
FirstName,LastName,DOB,POSTCODE

You want this output 1:

FirstName,DOB
FirstName,DOB
FirstName,DOB
FirstName,DOB

And this putput 2:

FirstName,POSTCODE
FirstName,POSTCODE

Then you can try simply this:

with open('input.txt') as input, open('output1.txt', 'w') as oF1, open('output2.txt', 'w') as oF2 :

    for line in input:
        chars = line.split(',')

        if chars[2] == "2000":
            oF2.write(chars[0] + "," + chars[3])
        else:
            oF1.write(chars[0] + "," + chars[2] + "\n")

Upvotes: 1

Jan Spurny
Jan Spurny

Reputation: 5537

You can open both output files before iterating over lines in iF (that's why your version didn't produce any POSTCODE.txt - because the iF was already iterated over and there was no lines left).

This should work:

def func(iFile):
    with open(iFile) as iF:
        with open("DOB.txt", "a") as oF1:
            with open("POSTCODE.txt", "a") as oF2:
                for line in iF:
                    chars = line.split(',')
                    oF1.write(chars[0] + ',' + chars[2])
                    oF1.write('\n')
                    if chars[2] == "2000":
                        oF2.write(chars[0] + ',' + chars[3])
                        oF2.write('\n')

Upvotes: 2

Arkaitz Jimenez
Arkaitz Jimenez

Reputation: 23198

You are opening the DOB.txt the second time as open("DOB.txt", "a") which is meant for appending, not for reading.

No surprise on the empty POSTCODE.txt since you won't be able to read it when you open it the second time.

Upvotes: 0

Newb
Newb

Reputation: 2930

I know this technique is somewhat deprecated, but you could just do:

file1 = open("firstfile.txt","w")
file2 = open("secondfile.txt","w")

and then use file1.write(...) and file2.write(...) to split the output correctly. The with syntax might be a bit tricky in this sort of context.

Also, "a" is for append. Be careful that you don't use "a" when you mean "w" and vice-versa.

Upvotes: 0

Related Questions