1

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.

1
  • why not use something like outputnames = ','.join(participants2) before writing to file, making it a CSV. you can change the , to any character you disallow in names Commented Jan 2, 2017 at 18:29

3 Answers 3

4

One better way to do this is writing the names to the file one per line:

fileWrite.write('\n'.join(participants2))

Then you can retrieve the list of names via readlines:

participants2 = fileRead.readlines()  # elements will will need to be stripped

But, the proper way of serializing data to files is to use a tested library like pickle or json:

import json 
# import pickle

with open('file.json', 'w') as f:
    json.dump(participants, f)
    # pickle.dump(participants, f)

Then you can retrieve the list via:

with open('file.json', 'r') as f:
    participants = json.load(f)
    # participants = pickle.load(f)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! The first one worked and I went with that for simplicity for now. But I did make a note of your other, 'proper' solution. Some great information you posted :D
2

Don't write the list as string. There are a number of ways that you could do this. The most common would be to either write each user on a different line of the file, or to use a csv (or some other kind of delimiter).

import csv

with open('file_name.csv', newline='') as fh:
    writer = csv.writer(fh)
    writer.writerow(participants2)

Comments

0

write list first via join: file.write(','.join(list))

read it and use split to get list back:

data = file.read() #read data list = data.split(',')#split data

for multiple list different levels of seperation may work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.