[SOLVED] Turn every line of txt into list with strings

Issue

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]

Answered By – erisch

Answer Checked By – Jay B. (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *