tutorial_3.py 861 B

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