2

I want to get a lot of usernames from file - each line is one username. Currently I use this code:

lista = []
z = open("usrnames.txt")
lista = z.readlines()

But then the results, which are saved in list are:

['username1\n', 'username2\n', 'username3\n']

I do not want to have EOLs... I except this result:

['username1', 'username2', 'username3']

How can I do that? I read documentation of this function, and didn't find any argument, which can disable putting EOLs...

TN888
  • 7,659
  • 9
  • 48
  • 84

1 Answers1

6

You could either use list comprehension with strip:

lista = [line.strip() for line in z.readlines()]

or simply, as suggested by @Matthias:

lista = [line.strip() for line in z]

or use splitlines:

lista = z.read().splitlines()
fredtantini
  • 15,966
  • 8
  • 49
  • 55