简体   繁体   中英

What does line.split do?

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']

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']

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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM