您好,我编写了一个脚本,该脚本比较两个文本文件并将差异输出到单独的文本文件(difference.txt)中。我的目标是让记录的名称来自于每一行的开头或每一行的结尾,然后移至我的difference.txt中的下一行。
我的脚本相当简单:
Jan = "JanFile.txt"
Feb = "FebFile.txt"
rootFolder = os.path.dirname(os.path.abspath(__file__))
with open (Jan, "rt") as doc1, open(Feb, "rt") as doc2:
prevMonth = set(doc1.readlines())
currentMonth = set(doc2.readlines())
with open ('TxtDiff.txt', 'wt') as unmatchedFile:
line = (currentMonth - prevMonth)
unmatchedFile.writelines(line)
只需重申一下:我想知道在difference.txt文件中记录来自哪个文件。无论如何,是否要追加此信息,使其显示在行之前或之后
例如:
JanFile看起来像:
Column1 | Column2
Data1 | Data2
FebFile看起来像:
Column1 | Column2
Data1 | Data2
Difference.txt如下:
Column1 | Column2 | Column3
JanFile | Data1 | Data2
FebFile | Data1 | Data2
FebFile | Data1 | Data2
JanFile | Data1 | Data2
我非常坚持使用 line =(currentMonth-prevMonth),因为使用diff,pandas,xlrd等产生了许多其他问题。
在此问题上的任何帮助将不胜感激。 谢谢