0

I need to read from 3 txt files and merge them into one big txt file.

Ex text file1:

John
Mary
Joe

Ex text file2:

Alabama
Alaska
Michigan

Ex text file3:

Maybe
Attending
Not Attending

I'm not sure what else to add to my code

path = '/home/pi/Documents/Test/name.txt'
file1 = open (path, 'r')
name = file1.read()

statepath = '/home/pi/Documents/Test/state.txt'
file2 = open (path, 'r')
states = file2.read()

statuspath = '/home/pi/Documents/Test/status.txt'
file3 = open(statuspath, 'r')
status = file3.read()

finalpath = '/home/pi/Documents/Test/final.txt'
file4 = open(finalpath, 'w')
final = file4.read()


for item in name, states, status:
    final.write(file1, "\n")
    final.write(file2, "\n")
    final.write(file3, "\n")

file1.close()
file2.close()
file3.close()
final.close()

final expected output of the file is

John  <------- first value in file1
Alabama <------ first value in file2
Maybe  <------- first value in file 3
Mary  <---------- second value in file 1
Alaska
Attending
Joe
Michigan
Not Attending

Basically trying to loop through all of them and print them sequentially not sure how to loop.

2
  • 2
    what is your expected output? Commented Jan 21, 2018 at 17:15
  • didnt notice that , i just added to it.. not sure how to loop through the lists to write them all to a file. Commented Jan 21, 2018 at 17:27

4 Answers 4

1

First of all you are writing in final without actually ever reading anything so it can't work. Replace file1, file2, file3 with the variables that have the read() attribute.


Just use a for statement with each variable you want to loop. Like this:

for i in name:
    for j in states:
        for k in status:
            all = i + '\n` + j + '\n' + k + '\n' 

final.write(all)
Sign up to request clarification or add additional context in comments.

Comments

1

One of possible solution, but you should be sure that you have the same length of 3 files.

def main():
    name_path = 'name.txt'
    state_path = 'state.txt'
    status_path = 'status.txt'
    final_path = 'final.txt'
    with open(name_path, 'r') as file1, open(state_path, 'r') as file2, open(status_path, 'r') as file3, open(final_path, 'w') as final:
        for line in file1.readlines():
            final.write(line)
            final.write(file2.readline())
            final.write(file3.readline())

3 Comments

i tried using this and it seems to stop after 4 loops... i have 900 lines in each file.
@ClaudeSAugilar are you sure? I just rechecked it and it works. However try to use another version that I posted, it's more readable and pythonic
you're welcome, you can mark the right answer that helped you, for other people to see working answer
0

Some way of doing this for a general case, using itertools:

import itertools as it
files = [
    '/home/pi/Documents/Test/name.txt',
    '/home/pi/Documents/Test/state.txt',
    '/home/pi/Documents/Test/status.txt'
]

def loadData(fpath):
    with open(fpath, "r") as f:
        yield from f.redlines()

with open('/home/pi/Documents/Test/final.txt') as f:
    for e in it.chain.from_iterable(zip(*map(loadDAta, files))):
        f.write(e)

Comments

0

I just slightly improved Netwave version and it seems to be the right pythonic way to solver this task, the full code will be something like this

import itertools as it


def load_data(fpath):
    with open(fpath, 'r') as f:
        for line in f.readlines():
            yield line


def main():
    files = [
        '/home/pi/Documents/Test/name.txt',
        '/home/pi/Documents/Test/state.txt',
        '/home/pi/Documents/Test/status.txt'
    ]

    with open('/home/pi/Documents/Test/final.txt', 'w') as f:
        for e in it.chain.from_iterable(zip(*map(load_data, files))):
            for line in e:
                f.write(line)


if __name__ == '__main__':
    main()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.