-1

I want to import a text file into my Python file and add each name in each line as a string to a list through a for loop. This is my text file:

A
B
C
D

I tried this:

text_file = open("01.txt", "r")
list1 = text_file.readlines()
list=[]
for name in list1:
    list.append(name)
print (list)

But the output is this:

['A\n', 'B\n', 'C\n', 'D']

I want the output to look like this:

['A', 'B', 'C', 'D']

How can I do this?

Vexter
  • 5
  • 3

2 Answers2

0
text_file = open("01.txt", "r")
list1 = text_file.read().splitlines()
print(list1)
Verfosec
  • 26
  • 4
0

Another option that involves fewer changes, is to change from

list.append(name)

to

list.append(name.strip())
Ryan W
  • 98
  • 2
  • 9