Mar-17-2021, 09:52 PM
def printGames(): with open("boardgames.csv", 'r') as csvfile: reader = csv.DictReader(csvfile) for row in reader: print(row) # print(line['title'] + ' ' + line['time'] + # ' ' + line['age'] + " " + line['players']) csvfile.close()
class Game: def __init__(self, title, players, age, time): self.title = title self.players = players self.age = age self.time = time def add_game(self): with open("boardgames.csv", 'w') as csvfile: fieldnames = ['title', 'time', 'age', 'players'] writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writerow({'title': str(self.title), 'time': str(self.time), 'age': str(self.age), 'players': str(self.players)}) csvfile.close()I have a problem with this code.
The print function doesnt work for some reason and in add_game if instead of w i type a, the programm doesnt store the dict properly why is that. Do i have a mistake or tehre is a bug with csv?