I’m beginner in python, I have two space delimited text files,
the first looks like:
a b c
d e f
the second looks like:
1 2 3
4 5 6
I want to concatenate them vertically such that the output is:
a b c
d e f
1 2 3
4 5 6
how can I do that using python?
and if I want to concatenate horizontally so it looks like this:
a b c 1 2 3
d e f 4 5 6
how can I do it also?
>Solution :
Maybe something like that, using join():
with open("File1", 'r') as f:
file1 = f.read().split("\n")
with open("File2", 'r') as f:
file2 = f.read().split("\n")
vertically = "\n".join(file1 + file2)
print(vertically)
horizontally = "\n".join([" ".join(line) for line in zip(file1, file2)])
print(horizontally)
To save:
with open("File3.txt", 'w') as f:
f.write(vertically)
with open("File4.txt", 'w') as f:
f.write(horizontally)