-4

I'm wondering what line.split does because I've been told it will help my code. I'm trying to make a list consisting of the current data stored inside of an external text file. My code goes as follows:

highscores = []

highscorefile = open('highscores.txt','r')
cont = highscorefile.readlines()
for line in cont:
    highscores.append(line)
highscorefile.close()
print(highscores)

I've been told that line.split will help sort it but I first need to figure out what it does.

Current output:

['1,3\n', '3,4\n', '6,5\n', '12,10']
martineau
  • 119,623
  • 25
  • 170
  • 301
SpickleMickle
  • 11
  • 1
  • 4

2 Answers2

0

split() method returns a list of strings after breaking the given string by the specified separator.

word = 'geeks, for, geeks, pawan'

maxsplit: 0

print(word.split(', ', 0)) 

Output

['geeks, for, geeks, pawan']
Sam
  • 387
  • 4
  • 17
0

split is a method used on strings. It splits a string by a seperator. For example,

'hello world how are you'.split(' ') = ['hello', 'world', 'how', 'are', 'you']
cmxu
  • 954
  • 5
  • 13