12345678910111213141516171819202122232425262728293031323334353637383940 |
- def new_file_sort():
- if not os.path.exists('analytics'):
- os.mkdir('analytics')
- new = []
- head = []
- with open('studentinfo_cs384.csv', newline='') as csvfile:
- reader = csv.DictReader(csvfile)
- for row in reader:
- head = list(row.keys())
- del head[1]
- head.insert(1,'first_name')
- head.insert(2,'last_name')
- k = list(row.values())
- del k[1]
- k.insert(1,row['full_name'].split()[0])
- k.insert(2,' '.join(row['full_name'].split()[1:]))
- new.append(k)
- csvfile.close()
- with open('analytics/studentinfo_cs384_names_split.csv', newline='',mode='w') as f:
- f_write = csv.writer(f, delimiter=',',lineterminator='\r')
- f_write.writerow(head)
- for i in new:
- f_write.writerow(i)
- f.close()
- #sorting
- dic = {}
- for i in new:
- dic[i[1]]='#$%^&*'.join(i)
- new = []
- with open('analytics/studentinfo_cs384_names_split_sorted_first_name.csv', mode = 'w') as f:
- print
- f.close()
- for i in sorted(dic.items()):
- new.append(i[1].split('#$%^&*'))
- with open('analytics/studentinfo_cs384_names_split_sorted_first_name.csv', mode = 'a') as f:
- f_write = csv.writer(f, delimiter=',',lineterminator='\r')
- f_write.writerow(head)
- for i in new:
- f_write.writerow(i)
- f.close()
|