import json with open log_file txt as lines readlines dings_list macs_

 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
import json
with open('log_file.txt', 'r') as f:
lines = f.readlines()
dings_list = []
macs_list = []
new_line = None
for line in lines[1:]:
new_line = line[:line.rfind(',')]
almost_json = new_line.replace('""', '"')
#print(almost_json)
new_json = json.loads(almost_json[1:-1])
ding_id = new_json['ding_id']
mac = new_json['device_id']
#print(ding_id)
dings_list.append(ding_id)
macs_list.append(mac)
with open('dings.txt', 'w') as f:
f.write('\n'.join(dings_list))
with open('macs+dings.csv', 'w') as f:
f.write('mac,ding\n')
result = zip(macs_list, dings_list)
for x, y in result:
f.write(f'{x},{y}\n')