This assumes that in your current working directory file1.txt
contains:
/home/picture/I10045.jpg
/home/picture/I10056.jpy
and file2.txt
contains
Cat, Dog
Mouse, Mouse, Mouse
It also assumes that we don't care about the order of the elements in each line of file2.txt
since it uses set
to remove duplicates. If you need that order i'd consider using a for
loop instead of a comprehension and manually building a list while checking membership with in
or making some unconventional use of OrderedDict
, there's some more details on how to do that stuff in here: Removing duplicates in lists
#!/usr/bin/env python3
with open("file1.txt") as file1, open("file2.txt") as file2:
file1_lines = [line.strip("\n") for line in file1]
file2_lines = [set(line.strip("\n").split(", ")) for line in file2]
with open("file3.txt", "w") as file3:
for line1, line2 in zip(file1_lines, file2_lines):
print(line1, ", ".join(line2), file=file3)
The contents of file3.txt
:
/home/picture/I10045.jpg Dog, Cat
/home/picture/I10056.jpy Mouse
An explanation of what's happening:
We open both input files using with
, which is usually recommended.
We run a list comprehension on the open file1
object which just removes the newlines from each line, this will help when we join the lines together later.
We run another list comprehension over our open file2
object which removes newlines and then splits each line on commas into a set
. This removes any duplicates and leaves us with a list of sets.
We open file3.txt
for writing and use zip
to allow us to iterate over both the lists we just made.
we use join
to rebuild the lines in file2.txt
with commas from the sets that are in file2_lines
. We don't have to do anything special to the lines from file1.txt
.
We use print
with the file=
argument to write to our file.. it's worth noting that this is file=
won't work in python2 without importing print_function
from __future__
.. if you're using python2 you should probably just use file3.write()
instead.