在检查事件的同时读取和写入文本文件

时间:2020-01-21 00:03:46

标签: python

我正在尝试从名为first-names.txt的文本文件中读取名称,并查看它们是否存在于oliver-twist.txt中。

到目前为止,我已经可以使用以下代码将不在occurrences.txt中但在oliver-twist.txt中存在的名称输出到first-names.txt

with open('first-names.txt', 'r')as f:
    d = set(f.readlines())

with open('oliver-twist.txt', 'r') as f:
    e = set(f.readlines())

with open('occurrences.txt', 'a') as f:
    for line in list(d-e):
        f.write(line)

来自oliver-twist.txt的摘录:

This resistance only infuriated Mr. Sikes the more; who, dropping on
his knees, began to assail the animal most furiously.  The dog jumped
from right to left, and from left to right; snapping, growling, and
barking; the man thrust and swore, and struck and blasphemed; and the
struggle was reaching a most critical point for one or other; when, the
door suddenly opening, the dog darted out:  leaving Bill Sikes with the
poker and the clasp-knife in his hands.

来自first-names.txt的摘录:

Aaron
Aaron
Abbey
Abbie
Abby
Abdul
Abe
Abel
Abigail
Abraham
Abram
Ada
Adah
Adalberto
Adaline
Adam
Adam
Bill

预期输出应为:

Bill

因为比尔是oliver-twist.txt中唯一的名字。 如何找到相同的事件而不是文件中的差异?

2 个答案:

答案 0 :(得分:0)

类似的事情应该起作用:

with open('first-names.txt') as f:
    first_names = f.readlines()

with open('oliver-twist.txt') as f:
    oliver_twist = f.read()

for name in first_names:
    if name in oliver_twist:
        print(name)

答案 1 :(得分:0)

您可以对从文本文件导入的两个集合进行全面了解,我在示例中添加了更多名称进行测试,所有名称看起来都可以被检测到。

在这里利用带删除导入的\n,如果文件中没有新行,则可能不需要。

with open('first-names.txt', 'r')as f:
    d = set(f.readlines())

with open('oliver-twist.txt', 'r') as f:
    e = set(f.readlines())

result = [name.strip() for name in d for sentence in e if name.strip() in sentence]

#['Abram', 'Ada', 'Adah', 'Bill', 'Adam']

我将所有名称添加到oliver-twist.txt,并将bill添加到first-names.txt,因为示例中没有匹配项。