how to combine common values from 2 txt files into 1 file using python
Question:
I’m not sure how to write a script to combine values in 2 separate txt files using the specific tag of not done
and ensure there are no duplicates. I currently have 2 files with lists of filenames.
Each of them have ‘tags’ that indicate whether they ‘exist’ or not.
Here is an example:
File1.txt:
P03T10C01 …[not done]
P02T02C03…exists
P12T06C04…exists
P02T02C06…exists
P06T09C01…exists
P03T19C04 …[not done]
P04T08C01 …[not done]
File2.txt:
P15T05C05 …[not done]
P15T05C04…exists
P03T10C01 …[not done]
P12T06C04…exists
P06T09C01…exists
P03T19C05 …[not done]
P03T19C04 …[not done]
Since the values
"P03T10C01" and "P03T19C04 "
are common in both files, it should appear in my result.txt
once. The others that have [not done]
attached to it, should also be in the result.txt
.
Result.txt:
P03T19C04 …[not done]
P04T08C01 …[not done]
P03T10C01 …[not done]
P03T19C05 …[not done]
P15T05C05 …[not done]
How does a script for this look like? Any help is appreciated thank you!
Answers:
The below script will read the textfiles, find the common elements and write it on another file
txt1, txt2 = '', ''
with open("path/to/txtfile1.txt", "r") as f1:
txt1 = f1.read()
with open("path/to/txtfile2.txt", "r") as f2:
txt2 = f2.read()
txtlist1 = [x for x in txt1.split("n") if "not done" in x]
txtlist2 = [x for x in txt2.split("n") if "not done" in x]
print(len(txtlist1), len(txtlist2))
set1 = set(txtlist1)
set2 = set(txtlist2)
result = 'n'.join(list(set1.union(set2)))
with open("path/to/txtfile3.txt", "w") as f3:
f3.write(result)
Result
P03T10C01 ...[not done]
P03T19C04 ...[not done]
P03T19C05 ...[not done]
P04T08C01 ...[not done]
P15T05C05 ...[not done]
I’m not sure how to write a script to combine values in 2 separate txt files using the specific tag of not done
and ensure there are no duplicates. I currently have 2 files with lists of filenames.
Each of them have ‘tags’ that indicate whether they ‘exist’ or not.
Here is an example:
File1.txt:
P03T10C01 …[not done]
P02T02C03…exists
P12T06C04…exists
P02T02C06…exists
P06T09C01…exists
P03T19C04 …[not done]
P04T08C01 …[not done]
File2.txt:
P15T05C05 …[not done]
P15T05C04…exists
P03T10C01 …[not done]
P12T06C04…exists
P06T09C01…exists
P03T19C05 …[not done]
P03T19C04 …[not done]
Since the values
"P03T10C01" and "P03T19C04 "
are common in both files, it should appear in myresult.txt
once. The others that have[not done]
attached to it, should also be in theresult.txt
.
Result.txt:
P03T19C04 …[not done]
P04T08C01 …[not done]
P03T10C01 …[not done]
P03T19C05 …[not done]
P15T05C05 …[not done]
How does a script for this look like? Any help is appreciated thank you!
The below script will read the textfiles, find the common elements and write it on another file
txt1, txt2 = '', ''
with open("path/to/txtfile1.txt", "r") as f1:
txt1 = f1.read()
with open("path/to/txtfile2.txt", "r") as f2:
txt2 = f2.read()
txtlist1 = [x for x in txt1.split("n") if "not done" in x]
txtlist2 = [x for x in txt2.split("n") if "not done" in x]
print(len(txtlist1), len(txtlist2))
set1 = set(txtlist1)
set2 = set(txtlist2)
result = 'n'.join(list(set1.union(set2)))
with open("path/to/txtfile3.txt", "w") as f3:
f3.write(result)
Result
P03T10C01 ...[not done]
P03T19C04 ...[not done]
P03T19C05 ...[not done]
P04T08C01 ...[not done]
P15T05C05 ...[not done]