This question already has an answer here:
- How do I create a variable number of variables? 15 answers
I am reading text from a .txt file and need to use one of the data I read as a variable for a class instance.
class Sports:
def __init__(self,players=0,location='',name=''):
self.players = players
self.location = location
self.name = name
def __str__(self):
return str(self.name) + "is played with " + str(self.players) + " players per team on a/an " + self.location + "."
def makeList(filename):
"""
filename -- string
"""
sportsList = []
myInputFile = open(filename,'r')
for line in myInputFile:
record = myInputFile.readline()
datalist = record.split()
sportsList.append(datalist[0])
datalist[0] = Sports(int(datalist[1]),datalist[2],datalist[3])
myInputFile.close()
print(football.players)
makeList('num7.txt')
I need to convert datalist[0], which is a string, to a variable name (basically without the quotes) so it can be used to create an instance of that name.