如何检查两个文件中的行是否包含在一起

时间:2016-08-20 11:38:56

标签: python

我是python的新手,比较两个文件时遇到问题,并以布尔形式获取输出。我在这里看到的建议很少,但由于应用知识较少,我认为我无法保持静止。需要比较这两个与权限相关的文本文件

1ST文本文件:perm.txt

ACCESS_COARSE_LOCATION
ACCESS_FINE_LOCATION
ACCESS_NETWORK_STATE
BLUETOOTH
CAMERA
CHANGE_WIFI_STATE
EXPAND_STATUS_BAR
GET_ACCOUNTS
GET_TASKS
MANAGE_DOCUMENTS
READ_EXTERNAL_STORAGE
READ_LOGS
RECORD_AUDIO
SET_WALLPAPER
USE_CREDENTIALS
VIBRATE
WRITE_CALENDAR

2ND TEXT FILE文件:op3.txt

GET_TASKS
EXPAND_STATUS_BAR
SET_WALLPAPER
CAMERA
MANAGE_DOCUMENTS
READ_EXTERNAL_STORAGE
ACCESS_COARSE_LOCATION
ACCESS_FINE_LOCATION
CHANGE_WIFI_STATE
VIBRATE
RECORD_AUDIO

我需要将第二个文件权限与第一个文件进行比较,并根据比较,如果两个文件中的权限相同,则输出应为“1”或“True”,否则应为“0”或“False”

我尝试使用以下代码

f1 = open('op2.txt', 'r')
f2 = open('permissions.txt', 'r')
FO = open('out1.txt', 'w')

for line1 in sorted(f2):
    if line1 is f1:
        FO.write(line1 + "True" + '\n')
    else:
        FO.write(line1 + "False" + '\n')

FO.close()
f1.close()
f2.close()

我尝试这样的另一次尝试能够得到两个或三个权限的输出我尝试了diff方式但最终无法得到它在这一点上来到这里

fname1 = input("Enter the first filename: ")
fname2 = input("Enter the second filename: ")


f1 = open(fname1)
f2 = open(fname2)


print("-----------------------------------")
print("Comparing files ", " > " + fname1, " < " + fname2, sep='\n')
print("-----------------------------------")


f1_line = f1.readline()
f2_line = f2.readline()

line_no = 1

# Loop if either file1 or file2 has not reached EOF
while f1_line != '' or f2_line != '':


f1_line = f1_line.rstrip()
f2_line = f2_line.rstrip()


if f1_line != f2_line:

    # If a line does not exist on file2 then mark the output with false
    if f2_line == '' and f1_line != '':
        print("false", "Line-%d" % line_no, f1_line)
    # otherwise output the line on file1 and mark it with > sign
    elif f1_line != '':
        print("True", "Line-%d" % line_no, f1_line)



    # Print a blank line
    print()

# Read the next line from the file
f1_line = f1.readline()
f2_line = f2.readline()


line_no += 1


f1.close()
f2.close()

3 个答案:

答案 0 :(得分:1)

如果您只需要快速快速的字符串比较,为什么不使用散列(md5 / sha1)?

对行进行排序,然后使用以下内容:

import md5

m1 = md5.md5(file1_str)
m2 = md5.md5(file2_str)

if m1.hexdigest() == m2.hexdigest():
    ....
else:
    ....

答案 1 :(得分:1)

功能

get_line_set = lambda f_name: set([l.strip() for l in open(f_name, 'r')])

创建f_name行的set

现在你可以使用

get_line_set('perm.txt') == get_line_set('op3.txt')

此外,集合允许您查看交集,差异,对称差异等。例如,要查看从perm.txtop3.txt的差异,只需将其更改为

即可
get_line_set('perm.txt').difference(get_line_set('op3.txt'))

答案 2 :(得分:0)

这样做的一种方法如下:

f1 = open('perm.txt', 'rb')
f2 = open('op3.txt', 'rb')
f1_lines = f1.readlines()
f2_lines = f2.readlines()
f1.close()
f2.close()
overall_compare_result = True
fo = open('out1.txt', 'w')
for each in f1_lines:
    if each in f2_lines:
        fo.write(each.strip() + ' True\n')
    else:
        fo.write(each.strip() + ' False\n')
        if overall_compare_result:
            overall_compare_result = False
fo.close()
print("overall comparision result: " + str(overall_compare_result))

out.txt将具有以下内容:

ACCESS_COARSE_LOCATION True
ACCESS_FINE_LOCATION True
ACCESS_NETWORK_STATE False
BLUETOOTH False
CAMERA True
CHANGE_WIFI_STATE True
EXPAND_STATUS_BAR True
GET_ACCOUNTS False
GET_TASKS True
MANAGE_DOCUMENTS True
READ_EXTERNAL_STORAGE True
READ_LOGS False
RECORD_AUDIO False
SET_WALLPAPER True
USE_CREDENTIALS False
VIBRATE True
WRITE_CALENDAR False