pythonstringfiletxtfile-processing

Turn every line of txt into list with strings


how to turn each line of txt file into list of strings

example sentences

board games for kids
house with view on the sea

I need output like this in python

sentences = ["board games for kids ","house  with view on the sea"]

Solution

  • with open('file.txt','r') as f:
        sentences = list(f)
    

    Or if you don't want the \n at the end just do

    with open('file.txt','r') as f:
        sentences = [line[:-1] for line in f]