0

(in txt file)

line

line

notice that there's a blank line between line

I would want the array to look like: x = [“line”, “line”, “notice that there's a blank line between line”]

Paul M.
  • 10,481
  • 2
  • 9
  • 15
Fexyk
  • 3
  • 1

2 Answers2

2

Try

txtFile = open("yourfile.txt").readlines()
x = [txtFile[i].strip() for i in range(len(txtFile)) if i % 2 == 0 ]

Edit: If you just want to remove blank lines try replacing the x variable line with

x = [txtFile[i] for i in range(len(txtFile)) if txtFile[i] != "\n"]
Eriall
  • 177
  • 1
  • 7
0

You can do it in this way -

with open("file_name.txt") as file:
    result = [line.rstrip() for line in file if line.rstrip()]
    print (result)

O/P: ['line', 'line', "notice that there's a blank line between line"]

Tabaene Haque
  • 576
  • 2
  • 10