tutorial_4.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. def email_domain_extract():
  2. if not os.path.exists('analytics'):
  3. os.mkdir('analytics')
  4. if os.path.exists('analytics/email'):
  5. shutil.rmtree('analytics/email')
  6. with open('studentinfo_cs384.csv', newline='') as csvfile:
  7. reader = csv.DictReader(csvfile)
  8. if not os.path.exists('analytics/email'):
  9. os.mkdir('analytics/email')
  10. for row in reader:
  11. l = list(row.values())
  12. head = list(row.keys())
  13. if '@' in row['email'] and '.' in row['email']:
  14. domain = row['email'].split('@')[1].split('.')[0]
  15. with open('analytics/email/'+domain+ '.csv', mode = 'a') as f:
  16. f_write = csv.writer(f, delimiter=',',lineterminator='\r')
  17. if os.path.getsize('analytics/email/'+ domain + '.csv')==0:
  18. f_write.writerow(head)
  19. f_write.writerow(l)
  20. f.close()
  21. else:
  22. with open('analytics/email/'+'misc'+ '.csv', mode = 'a') as f:
  23. f_write = csv.writer(f, delimiter=',',lineterminator='\r')
  24. if os.path.getsize('analytics/email/'+ domain + '.csv')==0:
  25. f_write.writerow(head)
  26. f_write.writerow(l)
  27. f.close()
  28. csvfile.close()