tutorial_6.py 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. def dob():
  2. if not os.path.exists('analytics'):
  3. os.mkdir('analytics')
  4. if os.path.exists('analytics/dob'):
  5. shutil.rmtree('analytics/dob')
  6. with open('studentinfo_cs384.csv', newline='') as csvfile:
  7. reader = csv.DictReader(csvfile)
  8. if not os.path.exists('analytics/dob'):
  9. os.mkdir('analytics/dob')
  10. for row in reader:
  11. l = list(row.values())
  12. head = list(row.keys())
  13. x = str(re.sub(r"\D","-",row['dob']))
  14. yr = int(x.split('-')[-1])
  15. k = int(yr)%10
  16. if k>4:
  17. name = 'bday_' + str(yr - k + 5) + '_' + str(yr - k + 9)
  18. else:
  19. name = 'bday_' + str(yr - k ) + '_' + str(yr - k + 4)
  20. if yr > 2014:
  21. name = 'bday_2015_2020'
  22. with open('analytics/dob/'+name+ '.csv', mode = 'a') as f:
  23. f_write = csv.writer(f, delimiter=',',lineterminator='\r')
  24. if os.path.getsize('analytics/dob/'+name+ '.csv')==0:
  25. f_write.writerow(head)
  26. f_write.writerow(l)
  27. f.close()