tutorial_5.py 874 B

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