简体   繁体   English

比较两个.txt文件中的行,打印出不包含单词的新行

[英]Compare lines in two .txt files, print out new line for not contained words

I have the following piece of code that, for every line in textfile1, searches textfile2 and if the line is contained in textfile2 prints out the corresponding line of textfile2. 我有以下代码,对于textfile1中的每一行,搜索textfile2,如果该行包含在textfile2中,则会打印出textfile2的相应行。 I want to however, print out new line for every line not contained in textfile2. 但是,我想为textfile2中未包含的每一行打印出新行。 Here is the code: 这是代码:

def readline():
with open("textfile1.txt") as file, open("textfile2.txt") as file2:
    string = set(map(str.rstrip,file))
    for line in file2:
        spl = line.split(None, 1)[0]
        if spl in string:
            print(line.rstrip())    
        else:              ##if spl not in string print new line
            print("\n")

It doesn't work as I expect (doesn't print out any new lines), what may be the problem or any alternative solutions? 它没有按我期望的那样工作(不打印任何新行),可能是什么问题或任何替代解决方案?

Sample Textfile1: 样本文本文件1:

'
a
aa
ab
abandon
abandonaudiofocus
abandonsession
abort
abortablehttprequest
abortanimation
abortcaptures
abortconnection
abortpolicy
abortrequest
abs

Sample Textfile2: 样本文本文件2:

'                |            22624
a                |               91
aa               |                7
ab               |                6
abort            |                8
abortanimation   |                5
abs              |              131
abslistview      |              115
absolutelayout   |               50
absolutesizespan |                6
abstracthttpentity |                2
abstractlist     |                1
abstractmap      |                4
abstractselector |                1
abstractset      |                2

Textfile1 includes many more words and it contains all the words in textfile2. Textfile1包含更多单词,并且包含textfile2中的所有单词。

For every line in textfile2 , searches first part of it in textfile1 and if the line is contained in textfile2 prints out the corresponding line of textfile2 . 对于每一行textfile2 ,搜索它的第一部分在textfile1并且如果线被包含在textfile2打印出的对应线textfile2

def readline():
        file1_list = [line.rstrip() for line in open("textfile1.txt")]
        file2_list = [line.rstrip() for line in open("textfile2.txt")]
        fileo_list = [line if line.split(None, 1)[0] in file1_list else '' for line in file2_list]
        for line in fileo_list:
            print(line)

This will print out: 这将打印出:

'                |            22624
a                |               91
aa               |                7
ab               |                6
abort            |                8
abortanimation   |                5
abs              |              131


.....

According to your question - 根据您的问题-

for every line in textfile1, searches textfile2 and if the line is contained in textfile2 prints out the corresponding line of textfile2 对于textfile1中的每一行,搜索textfile2,如果该行包含在textfile2中,则打印出textfile2的相应行

And comment - 并发表评论-

Textfile1 includes many more words and it contains all the words in textfile2 Textfile1包含更多单词,并且包含textfile2中的所有单词

The logic you have right now if actually opposite, it checks for each line in file2 - textfile2.txt - whether that line's first part exists in the file - textfile1.txt - which would always be true, according to your comment. 您现在拥有的逻辑(如果实际上相反)将检查file2每一行textfile2.txt该行的第一部分是否存在于file textfile1.txt根据您的评论,该行始终为真。

You need to get all elements (first part of each line) of file2 in the set and then check each line of file . 您需要获取集合中file2的所有元素(每行的第一部分),然后检查file每一行。 Example - 范例-

def get_first(line):
    return line.split(None, 1)[0]

def readline():
    with open("textfile1.txt",'r') as file, open("textfile2.txt",'r') as file2:
        string = set(map(get_first,file2))
        file2.seek(0)
        file2_dict = {}
        for line in file2:
            file2_dict[line.split(None, 1)[0]] = line
        for line in file:
            if line.strip() in string:
                print(file2_dict[line.rstrip()])    
            else:              ##if spl not in string print new line
                print()

Also, you do not need "\\n" inside your print() in else part, print also puts a newline by itself , you can just do - print() to print a newline. 另外,在print()的其他部分不需要"\\n" ,print本身也会放置换行符,您只需执行print()即可打印换行符。


Example/Demo - 示例/演示-

>>> def get_first(line):
...     return line.split(None, 1)[0]
...
>>> def readline():
...     with open("a.txt",'r') as file, open("b.txt",'r') as file2:
...         string = set(map(get_first,file2))
...         for line in file:
...             if line.strip() in string:
...                 print(line.rstrip())
...             else:              ##if spl not in string print new line
...                 print()
...
>>> readline()
a
aa
ab



abort

abortanimation




abs

In the above example, a.txt contains data from your example textfile1.txt and b.txt contains data from example of textfile2.txt . 在上面的示例中, a.txt包含来自示例textfile1.txt数据, b.txt包含来自textfile2.txt示例的数据。

Sets make this pretty easy 套装使这个变得非常容易

with open("textfile1.txt") as file1:
    textfile_1_set = set(map(str.rstrip, file1))

with open("textfile2.txt") as file2:
    textfile_2_set = set([l.split()[0] for l in file2])

# remove all the lines that are in textfile2 from the 
# set of lines from textfile1
in_1_but_not_2 = textfile_1_set - textfile_2_set

for line in in_1_but_not_2:
    print line

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较两个txt文件并在txt文件中逐行打印差异 - Compare two txt files and print the difference line by line in txt file 读取两个 csv 文件并比较每一行。 如果行匹配打印两行,如果不相似则打印无效 - Read two csv files and compare every line. If the lines match print both lines, if it isn't similar print invalid 如何比较两个数据框中的两列单词值,并创建一个包含匹配/包含单词的新列? - How to compare two column words values from two dataframes, and create a new column containing matching/contained words? Python - 比较两个txt-Files的内容并打印结果 - Python - Compare the content of two txt-Files and print the results Python,比较2个txt文件,找到第2个txt文件中唯一的行和output到一个新的txt文件 - Python, compare 2 txt files, find unique lines in the 2nd txt file and output to a new txt file 如何比较2个txt文件的差异并输出到新的txt文件并使用python打印到shell - how to compare difference in 2 txt files and output to a new txt file and print to shell using python 比较2个包含一行单词的文件 - Compare 2 files that contain a line of words 比较Python中的两个txt文件 - Compare two txt files in Python 比较两个文件.txt - Python - Compare two files .txt - Python Python:比较两个csv文件并打印出差异 - Python : Compare two csv files and print out differences
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM