如何使用python实现这一目标?
开发代码,它将采用两个输入文件,两个文件都按照字母顺序排列成相同的ASCII字符串,并产生两个输出文件 - 第一个输出文件应该只包含在第一个输入文件中找到的字符串,但不能在第二个;第二个输出文件 - 在第二个输入文件中找到的字符串,但不在第一个输入文件中。
答案 0 :(得分:2)
伪代码:
open both input files and both output files,
read a line from each input files into a & b,
while len(a) > 0 or len(b) > 0:
if both are the same output to samefile and read next line from both files,
if a > b:
output b to diff file
read next b
else: # a < b
output a to diff file
read next a
close all the files
编码留给OP。
答案 1 :(得分:1)
您可以使用设置操作
##file1.txt
##-----------------
##a new thing
##this is data
##and more data
##file2.txt
##--------------
##another new thing
##this is data
##and more data
infile1 = open('file1.txt', 'r')
infile2 = open('file2.txt', 'r')
file1_lines = infile1.readlines()
file2_lines = infile2.readlines()
out_lines_1 = set(file1_lines) - set(file2_lines)
out_lines_2 = set(file2_lines) - set(file1_lines)
outfile1 = open('outfile1.txt', 'w')
outfile2 = open('outfile2.txt', 'w')
outfile1.writelines(out_lines_1)
outfile2.writelines(out_lines_2)
infile1.close()
infile2.close()
outfile1.close()
outfile2.close()
答案 2 :(得分:0)
这有效:
from collections import Counter
with open(fn1,'r') as f1:
c1=Counter(e.strip() for e in f1)
with open(fn2,'r') as f2:
c2=Counter(e.strip() for e in f2)
col1=c1-c2
col2=c2-c1
col3=c1 & c2
print 'Only in first file:',','.join(sorted(col1.elements()))
print 'Only in second file:',','.join(sorted(col2.elements()))
print 'In both files:',','.join(sorted(col3.elements()))
如果您从Wikipedia's entry on comm获取两个示例文件,则会打印:
Only in first file: eggplant
Only in second file: banana,zucchini
In both files: apple,banana
答案 3 :(得分:-1)
没有为你做太多的设计(这里的人不会为你解决问题),我会说字典在这里是个不错的选择。关键可以是单词和条目从中读取的文件。然后,字典可以轻松识别仅在其中一个文件中找到的单词