Extract Words from a file

后端 未结 3 621
野的像风
野的像风 2021-01-13 23:03

I open a file using python to find whether a predefined set of words are present in the opened file or not. I took the predefined set of words in a list and opened the file

3条回答
  •  星月不相逢
    2021-01-13 23:34

    This code will show what words are present in the file, given that the word exactly matches, and is not preceded or followed by punctuation or other characters, and is of the same case. With some minor adjustment, the code could be made more forgiving.

    words = set(['hello', 'world', 'testing'])
    f     = open('testfile.txt', 'rb')
    data  = set(f.read().split())
    print words.intersection(data)
    

提交回复
热议问题