簡體   English   中英

Python:比較兩個文件

[英]Python : Compare two files

我有兩個輸入文件:

斯堪的納維亞T航空公司
一n 0航班號
六n 0航班號
2 n 0航班號
三n 0航班號

Speedbird T航空公司
一n 0航班號
六n 0航班號
八n 0航班號

我的第二個輸入文件:

斯堪的納維亞T航空公司
一n 0航班號
六n 0航班號
2 n 0航班號
三n 0航班號

六n 0航班號
八n 0航班號

我有以下代碼:

with open('output_ref.txt', 'r') as file1:
with open('output_ref1.txt', 'r') as file2:
same = set(file1).difference(file2)
print same
print "\n"

same.discard('\n')

with open('some_output_file.txt', 'w') as FO:
for line in same:
    FO.write(line)

我得到的輸出為:

斯堪的納維亞T航空公司
Speedbird T航空公司

但是我的實際輸出應該是:

斯堪的納維亞T航空公司
Speedbird T航空公司
一n 0航班號

有人可以幫助我解決問題嗎?

首先,如果您要從2文件中獲取公共行(“相同”變量名稱建議),那么您應該使用交集方法而不是difference。 而且,這兩種方法都被聲明為需要集合作為它們的參數,因此我將采取額外的步驟並將第二個文件也變成集合。 因此,新代碼應為:

 first = set(file1)
 second = set(file2)
 same = first.intersection(second)

.....

編輯:

閱讀對我的帖子的一些評論使我確信,您實際上想要的是差異,而不是集結,而是列表。 我想這應該為您工作:

difference = list(file1)
second = list(file2)
for line in second:
    try:
        first.remove(line)
    except ValueError,e:
        print e # alternately you could just pass here
def diff(a, b):
    y = []
    for x in a:
        if x not in b:
            y.append(x)
        else:
            b.remove(x)
    return y

with open('output_ref.txt', 'r') as file1:
    with open('output_ref1.txt', 'r') as file2:
        same = diff(list(file1), list(file2))
        print same
        print "\n"

if '\n' in same:
    same.remove('\n')

with open('some_output_file.txt', 'w') as FO:
    for line in same:
        FO.write(line)
$ python compare.py
['scandinavian t airline airline\n', 'speedbird t airline airline\n', 'one n 0 flightnumber\n']



$ cat some_output_file.txt 
scandinavian t airline airline
speedbird t airline airline
one n 0 flightnumber

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM