我對普通數據有疑問。我有下面的格式包括數據的三個文本文件:從3個文本文件和匹配行下的行輸出匹配行
cli= 111
mon= 45
cli= 584
mon= 21
cli= 23
mon= 417
現在我有以下程序whcih當我執行它,它給了我所有匹配的CLI。換句話說,它給了我在3個文本文件中出現的CLI。
with open ('/home/user/Desktop/text1.txt', 'r') as file1:
with open ('/home/user/Desktop/text2.txt', 'r') as file2:
with open ('/home/user/Desktop/text3.txt', 'r') as file3:
same = set(file1).intersection(file2).intersection(file3)
same.discard('\n')
with open ('/home/user/Desktop/common.txt', 'w') as file_out:
for line in same:
file_out.write(line)
我的問題是,我也可以輸出值(MON = 45)與CLI = 111?假設所有3個文本文件中都存在CLI = 111。我想要一個這樣的結果:
cli= 111
mon= 45
mon= 98
mon= 32
在此先感謝。 PS:以上示例數據僅爲1個文本文件。假設有3個文本文件。謝謝!
所以你想在每個cli出現在所有文件後的相應星期一? –
@Padraic坎寧安確切! – starshine
好的,好吧,用字典很容易,我會把東西扔在一起 –