Select some words from an existing file in Python

后端 未结 4 800
南方客
南方客 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条回答
  •  旧时难觅i
    2021-01-24 02:55

    Splitting the seperate words into a list (assuming they are seperated by spaces)

    f = open('text.txt').read().split(' ')
    

    Then to get a list of the words ending in "os", like you had:

    h=[w for w in f if w.endswith('os')]
    

提交回复
热议问题