1

I have a text file that contains this: List of values 531 70 5 17 22 876

What I am trying to do is merge sort those values. I know the code to merge sort an array but I am just having trouble putting it into an array. Here is my code.

f = open("testNumbers.txt", "r")
lines=f.readlines()

When I run it I get: ['List of values\n', '531\n', '70\n', '5\n', '17\n', '22\n', '876']

How do I remove everything and just leave me with numbers? Thank you

thiccdan69
  • 11
  • 3

1 Answers1

1

Try this if there are strings in between too.

f = open("testNumbers.txt", "r")
lines=f.readlines()
lines = [num.strip() for num in lines if num.strip().isdigit()]

Or this when text only at the beginning:

f = open("testNumbers.txt", "r")
lines=f.readlines()
lines = [num.strip() for num in lines[1::]]
Bibhav
  • 1,579
  • 1
  • 5
  • 18