tutorial_7.py 833 B

123456789101112131415161718192021
  1. def state():
  2. if not os.path.exists('analytics'):
  3. os.mkdir('analytics')
  4. if os.path.exists('analytics/state'):
  5. shutil.rmtree('analytics/state')
  6. with open('studentinfo_cs384.csv', newline='') as csvfile:
  7. reader = csv.DictReader(csvfile)
  8. if not os.path.exists('analytics/state'):
  9. os.mkdir('analytics/state')
  10. for row in reader:
  11. l = list(row.values())
  12. head = list(row.keys())
  13. with open('analytics/state/'+row['state'].lower()+ '.csv', mode = 'a') as f:
  14. f_write = csv.writer(f, delimiter=',',lineterminator='\r')
  15. if os.path.getsize('analytics/state/'+row['state'].lower() + '.csv')==0:
  16. f_write.writerow(head)
  17. f_write.writerow(l)
  18. f.close()
  19. csvfile.close()