First aprt of the secont task input file import random import csv def

 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
#First aprt of the secont task (input file)
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, shuffle=0):
with open ('new_csv_file.csv', 'w') as csvfile:
fieldnames = ['mac', 'ding_id']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
kye = list(elements.keys())
value = list(elements.values())
for index_mac, mac in enumerate(kye):
for index_id, ids in enumerate(value[index_mac]):
print(index_id, ids)
writer.writerow({'mac':mac, 'ding_id':ids})
if shuffle == 1:
with open('new_csv_file.csv', 'r') as r, open('shuffled_new_csv_file.csv', 'w') as w:
data = r.readlines()
header, rows = data[0], data[1:]
random.shuffle(rows)
rows = '\n'.join([row.strip() for row in rows])
w.write(header + rows)
create_csv(data_preparation(2,3),1)