-3

As of above, I can't find any other sources that would provide a working solution. Basically I want to take strings in a text file with this stored:

0
1
2
3
2
4

And store the numbers into a list of integers like this:

[0, 1, 2, 3, 2, 4]

Any help?

Max Voitko
  • 1,542
  • 1
  • 17
  • 32
LeFluph
  • 3
  • 1

1 Answers1

2

Open the file using open() and convert into a list like so:

with open("myfile.txt") as myfile:
    int_list = [int(l.strip()) for l in myfile]

This iterates over every line, strips the \n at the end, converts into an int, and puts it all into a list.

Bharel
  • 23,672
  • 5
  • 40
  • 80