import random import csv def data_preparation mac_qnt ding_nt elements

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import random
import csv
def data_preparation(mac_qnt, ding_nt):
elements = {}
macs = [f'{random.randrange(0, 10000, 1)}_mac' for count in range(mac_qnt) ]
for index, value in enumerate(macs):
first_bit = random.getrandbits(32)
elements.update({value:[f'{first_bit}_{item}_id' for item in range(ding_nt)]})
print(elements)
return elements
def create_csv(elements):
l1 = []
with open ('new_csv_file.csv', 'w') as csvfile:
fieldnames = ['mac', 'ding_id']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for mac, ding in elements.items():
for val in ding:
l1.append({'mac':mac, 'ding_id':val})
random.shuffle(l1)
writer.writerows(l1)
create_csv(data_preparation(2,3))
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()