def data_split with open new_csv_file csv as csv_file reader csv DictR

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
def data_split():
with open('new_csv_file.csv', 'r') as csv_file:
reader = csv.DictReader(csv_file)
data = [{'mac': item['mac'], 'ding_id': item['ding_id']} for item in reader]
mac = set([item['mac'] for item in data])
for m in mac:
with open(f'{m}.csv', 'w') as f:
f_n = ['mac', 'ding_id']
writer = csv.DictWriter(f, fieldnames=f_n)
writer.writeheader()
for item in data:
if item['mac'] == m:
writer.writerow({'mac': item['mac'], 'ding_id': item['ding_id']})
data_split()