lundi 3 février 2020

How can I read a line from a file and split it

I am stuck on a bit of code and I can't get it to work.

from random import randint

def random_song():
    global song
    linenum = randint(1,43)
    open('data.txt')
    band_song = readlines."data.txt"(1)
    global band
    band = band_song.readlines(linenum)
    song = band_song.split(" ,")

What I'm trying to do is generate a random number between the 1st and last line of a text file and then read that specific line. Then split the line to 2 strings. Eg: line 26, "Iron Maiden,Phantom of the Opera" split to "Iron Maiden" and then "Phantom of the Opera

Also, how do I split the second string to the first letter of each word and to get that to work for any length and number of letters per word & number of words?

Thank you, MiniBitComputers




Aucun commentaire:

Enregistrer un commentaire