Queer European MD passionate about IT

pizza.py 1017 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import csv
  2. import os
  3. import sys
  4. import tabulate
  5. def main():
  6. try:
  7. file_name = sys.argv[1]
  8. except IndexError:
  9. print("Too few command-line arguments")
  10. sys.exit(1)
  11. if len(sys.argv) > 2:
  12. print("Too many command-line arguments")
  13. sys.exit(1)
  14. if not os.path.isfile(file_name): # Or try opening and catch FileNotFoundError
  15. print(f"File does not exist")
  16. sys.exit(1)
  17. if not file_name.endswith('.csv'):
  18. print(f"Not a CSV file")
  19. sys.exit(1)
  20. grid = get_grid_from_csv(file_name)
  21. print(grid)
  22. def get_grid_from_csv(file_name: str) -> str:
  23. headers = None
  24. table = []
  25. with open(file_name, 'r') as file_object:
  26. reader = csv.DictReader(file_object)
  27. for row in reader:
  28. if headers is None:
  29. headers = list(row.keys())
  30. table.append(tuple(row[field] for field in row))
  31. return tabulate.tabulate(table, headers, tablefmt="grid")
  32. if __name__ == "__main__":
  33. main()