打印包含txt文件中所有单词的单词的行

时间:2018-04-05 13:32:45

标签: python

我有制表符分隔文件,包含57k行和23列包含单词。

我想打印我的大文件中的所有行,这些行包含来自我的txt文件的单词。

我的文件:

with open('file.txt', 'r') as file:
for l in file:
    lines = l.strip().split('\t')

上述文件的示例输出:

['aaa', 'bbb', 'ccc']

现在,我希望使用以下句子打印我的大型.txt文件中的所有行:

Here aaa is a good day.
Your bbb is very good.
Here ccc.

怎么做?

最佳

3 个答案:

答案 0 :(得分:0)

我不理解answare

rows=open('file.txt', 'r').readlines()
rows=[a.split("\t") for a in rows]

for row in rows:
    print("here " + row + " is good day")

或者,用于搜索所有行中的单词

lista=[]
word="parola"
rows=open('file.txt', 'r').readlines()
for row in rows:
    if word in row: lista.append(row.split("\t"))

for row in word:
    print("here " + row + " is good day")

答案 1 :(得分:0)

获取所有单词,然后检查它们的存在

with open('file1.txt', 'r') as f:
    words = []
    for l in f:
        words.extend(l.strip().split())

with open('file2.txt', 'r') as f:
    for line in f:
        for w in words:
            if w in line:
                print(line)
                break

答案 2 :(得分:0)

我想你想找到两个文件之间的单词交集。 最简单的选择是使用集合:

words = set()
for line in open("file.txt"):
    words.update(line.strip().split("\t"))

for line in open("sentences.txt"):
    stripline = line.strip()
    line_words = set(stripline.split(" "))
    intersection = line_words & words
    if intersection:
        print stripline