tutorial_2.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. def course():
  2. if not os.path.exists('analytics'):
  3. os.mkdir('analytics')
  4. if os.path.exists('analytics/course'):
  5. shutil.rmtree('analytics/course')
  6. d = {'01':'btech',
  7. '11':'mtech',
  8. '21':'phd',
  9. '12':'msc'}
  10. with open('studentinfo_cs384.csv', newline='') as csvfile:
  11. reader = csv.DictReader(csvfile)
  12. if not os.path.exists('analytics/course'):
  13. os.mkdir('analytics/course')
  14. for row in reader:
  15. if len(row)==0:
  16. print(1)
  17. continue
  18. l = list(row.values())
  19. head = list(row.keys())
  20. stream = str(row['id'][-4:-2]).lower()
  21. yr = str(row['id'][:2])
  22. if str(row['id'][2:4]) in list(d.keys()):
  23. degree = d[str(row['id'][2:4])]
  24. else:
  25. with open('analytics/course/' + 'misc.csv' , mode = 'a') as f:
  26. f_write = csv.writer(f, delimiter=',',lineterminator='\r')
  27. if os.path.getsize('analytics/course/' + 'misc.csv')==0:
  28. f_write.writerow(head)
  29. f_write.writerow(l)
  30. f.close()
  31. continue
  32. csv_name = f'{yr}_{stream}_{degree}.csv'
  33. p = re.compile(r'\d\d\d\d\D\D\d\d')
  34. k = re.fullmatch(p,row['id'])
  35. if k:
  36. if not os.path.exists('analytics/course/'+ stream):
  37. os.mkdir('analytics/course/'+ stream)
  38. if not os.path.exists('analytics/course/'+ stream + '/' + degree):
  39. os.mkdir('analytics/course/'+ stream + '/' + degree )
  40. with open('analytics/course/'+ stream + '/' + degree + '/' + csv_name , mode = 'a') as f:
  41. f_write = csv.writer(f, delimiter=',',lineterminator='\r')
  42. if os.path.getsize('analytics/course/'+ stream + '/' + degree + '/' + csv_name)==0:
  43. f_write.writerow(head)
  44. f_write.writerow(l)
  45. f.close()
  46. else:
  47. with open('analytics/course/' + 'misc.csv' , mode = 'a') as f:
  48. f_write = csv.writer(f, delimiter=',',lineterminator='\r')
  49. if os.path.getsize('analytics/course/' + 'misc.csv')==0:
  50. f_write.writerow(head)
  51. f_write.writerow(l)
  52. f.close()
  53. csvfile.close()