Select some words from an existing file in Python

后端 未结 4 770
南方客
南方客 2021-01-24 02:14

I want to separate the exact words of a text file (text.txt) ending in a certain string by using \'endswith\'. The fact is that my variable

h=[w for w i         


        
4条回答
  •  广开言路
    2021-01-24 02:44

    Open the file first and then process it like this:

    with open('text.txt', 'r') as file:
            content = file.read()
    h=[w for w in content.split() if w.endswith('os')]
    

提交回复
热议问题