I'm writing a tournament creator for a game that lacks it, just for personal use. It's a good exercise for school... however, I've stumbled upon a problem while finishing up the program.
So after the participants for the tournament have been decided, I ask the user (myself, my cousin too at most, if he comes over) if he wants to save the current list of participants. If so, a list containing the participants gets written to a file:
fileWrite = open('Previous Participants', 'w')
fileWrite.write(str(participants2))
fileWrite.close()
however, this converts the list to a string, so if I want to read it out the next time the program is run... I have a string. Not a list and I need the list for the tournament creator itself (randomizing the fights, etc).
So... how do I get this string back as a list? The problem is, I can use split, I think, but I believe that participant names with spaces would then be a problem.
Currently the file has this string from last time:
['Cell', 'Lord Slug', 'Time Patroller', 'Gotenks', 'Omega Shenron', 'Nail', 'Time Breaker Bardock', 'Piccolo', 'Frieza', 'Mr. Satan', 'Beerus', 'Nappa', 'Raspberry', 'Goten', 'Vegito', 'Goku']
Participants like 'Lord slug' will cause a problem if I do:
ownCharacters = input('Do you wish to use the same participants as last time? ')
if ownCharacters == 'yes' or ownCharacters == 'Yes':
try:
fileRead = open('Previous Participants', 'r')
participants2 = fileRead.read()
participants2.split
except FileNotFoundError:
participants2 = participants2
Won't they? BTW participants2 has already been filled in with random names when the program arrives at this point, so if the file does not exist it should continue using the random names.
outputnames = ','.join(participants2)before writing to file, making it a CSV. you can change the , to any character you disallow in names