database.py 640 B

123456789101112131415161718192021222324252627
  1. import csv
  2. class Table:
  3. columnNames = []
  4. rows = []
  5. def __init__(self, columns):
  6. self.columnNames = columns
  7. def addRow(self, values):
  8. self.rows.append(values)
  9. def getLastRow(self):
  10. if len(self.rows) > 0:
  11. return rows[len(self.rows)-1]
  12. else:
  13. return [0] * len(self.columnNames)
  14. def getLastValue(self, column):
  15. col = self.columnNames.index(column)
  16. return self.getLastRow(col)
  17. def saveAsCsv(self, filename):
  18. with open(filename,"w+", newline='') as my_csv:
  19. csvWriter = csv.writer(my_csv,delimiter=',')
  20. csvWriter.writerow(self.columnNames)
  21. csvWriter.writerows(self.rows)