Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中比较和合并两个文件_Python_Python 2.7 - Fatal编程技术网

如何在python中比较和合并两个文件

如何在python中比较和合并两个文件,python,python-2.7,Python,Python 2.7,我有两个文本文件,名字是one.txt和Two.txt 在one.txt中,内容是 AAA BBB CCC DDD DDD EEE 在two.txt中,内容是 AAA BBB CCC DDD DDD EEE 我想要一个python代码来确定一个包含两个.txt的文件是否存在于一个.txt中 如果present意味着什么都不做,但如果two.txt的内容不是present意味着什么,它应该附加到one.txt 我想要一个1.txt格式的输出 AAA BBB CCC DDD EEE 代码:

我有两个文本文件,名字是one.txt和Two.txt 在one.txt中,内容是

AAA
BBB
CCC
DDD
DDD
EEE
在two.txt中,内容是

AAA
BBB
CCC
DDD
DDD
EEE
我想要一个python代码来确定一个包含两个.txt的文件是否存在于一个.txt中 如果present意味着什么都不做,但如果two.txt的内容不是present意味着什么,它应该附加到one.txt

我想要一个1.txt格式的输出

AAA
BBB
CCC
DDD
EEE
代码:

这很简单,因为它会为您处理副本

编辑

with open('file1.txt',"a+") as file1, open('file2.txt') as file2:
    new_words = set(file2) - set(file1)
    if new_words:
        file1.write('\n') #just in case, we don't want to mix to words together 
        for w in new_words:
            file1.write(w)
编辑2

如果订单很重要,请与Max Chretien一起回答

如果你想知道常用词,你可以使用交叉

with open('file1.txt',"a+") as file1, open('file2.txt') as file2:
    words1 = set(file1)
    words2 = set(file2)
    new_words = words2 - words1
    common = words1.intersection(words2)
    if new_words:
        file1.write('\n')
        for w in new_words:
            file1.write(w)
    if common:
        print 'the commons words are'
        print common
    else:
        print 'there are no common words'
这很简单,因为它会为您处理副本

编辑

with open('file1.txt',"a+") as file1, open('file2.txt') as file2:
    new_words = set(file2) - set(file1)
    if new_words:
        file1.write('\n') #just in case, we don't want to mix to words together 
        for w in new_words:
            file1.write(w)
编辑2

如果订单很重要,请与Max Chretien一起回答

如果你想知道常用词,你可以使用交叉

with open('file1.txt',"a+") as file1, open('file2.txt') as file2:
    words1 = set(file1)
    words2 = set(file2)
    new_words = words2 - words1
    common = words1.intersection(words2)
    if new_words:
        file1.write('\n')
        for w in new_words:
            file1.write(w)
    if common:
        print 'the commons words are'
        print common
    else:
        print 'there are no common words'
这应该做到:

with open('file1.txt', 'r+') as file1, open('file2.txt') as file2:
    f1 = [i.strip() for i in file1.readlines()]
    f2 = [j.strip() for j in file2.readlines()]
    f1 += [item for item in f2 if item not in f1]
    file1.seek(0)
    for line in f1:
        file1.write(line + '\n')
这应该做到:

with open('file1.txt', 'r+') as file1, open('file2.txt') as file2:
    f1 = [i.strip() for i in file1.readlines()]
    f2 = [j.strip() for j in file2.readlines()]
    f1 += [item for item in f2 if item not in f1]
    file1.seek(0)
    for line in f1:
        file1.write(line + '\n')
类似的解决方案使用的时间可能会短一点

with open('one.txt', 'r+') as f_one, open('two.txt', 'r') as f_two:
    res = sorted(set(f_one) | set(f_two))
    f_one.seek(0)
    f_one.writelines(res)
类似的解决方案使用的时间可能会短一点

with open('one.txt', 'r+') as f_one, open('two.txt', 'r') as f_two:
    res = sorted(set(f_one) | set(f_two))
    f_one.seek(0)
    f_one.writelines(res)

请直接在问题中编辑代码。请将代码添加到问题中,并缩进四个空格以保留格式;注释不适用于此,特别是对于依赖于空格的语言,如Python:)生成的文件需要有任何特定的顺序吗?在您的示例中,对两个源文件进行了排序——这是假设的吗?三个文件是否可以同时在内存中(因此文件不是很大)?无需按顺序格式化@Rory Daulton请直接在问题中编辑代码。请将代码添加到问题中,并缩进四个空格以保留格式;注释不适用于此,特别是对于依赖于空格的语言,如Python:)生成的文件需要有任何特定的顺序吗?在您的示例中,对两个源文件进行了排序——这是假设的吗?这三个文件可以同时在内存中吗(所以文件不是很大)?不需要按顺序格式化@Rory DaultonHow about
set(file1)| set(file2)
?实际上我想要的是file1.txt本身,不需要输出文件@Copperfield@stamaimer那也行,,但我认为,这一更新不会造成中间人的抛弃set@Copperfield,新词值将提供文件之间的差异,对吗?好奇的是,是否有任何方法可以获得file2.txt中已经存在的单词。@pavithranG是的,通过交叉点,您可以获得两个文件中都存在的单词如何
set(file1)| set(file2)
?实际上我想要的是file1.txt本身的结果,不需要输出文件@Copperfield@stamaimer那也行,,但我认为,这一更新不会造成中间人的抛弃set@Copperfield,新词值将提供文件之间的差异,对吗?好奇的是,有没有办法让已经出现的单词出现在file2.txt中?@pavithranG是的,通过交叉点,你可以得到两个文件中的单词