-4

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"]
  • 1
    Where are you getting the strings from? – Abhinav Mathur Mar 15 '22 at 11:10
  • Each line of a text file are already a list of strings – Sayse Mar 15 '22 at 11:13
  • I'm voting to close this question for the "needs debugging details" reason. This is a trivial file i/o task you should be able to attempt after reading a bare minimum of documentation and related questions. For this reason we expect you to share your attempt, not just a code writing request. – timgeb Mar 15 '22 at 11:22

2 Answers2

0
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]
erisch
  • 66
  • 3
0

lines = [line.rstrip() for line in open('file.txt')]