ClaudeSAugilar
ClaudeSAugilar

Reputation: 21

writing lists to text file in python3

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.

Upvotes: 0

Views: 2284

Answers (4)

Logovskii Dmitrii
Logovskii Dmitrii

Reputation: 2943

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()

Upvotes: 0

Logovskii Dmitrii
Logovskii Dmitrii

Reputation: 2943

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())

Upvotes: 1

Netwave
Netwave

Reputation: 42678

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)

Upvotes: 0

Xantium
Xantium

Reputation: 11605

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)

Upvotes: 1

Related Questions