import json with open my_file txt as file print len file readlines Rea

 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
import json
# 1
with open('my_file.txt') as file:
print(len(file.readlines()))
# 2
"""
Real is wall clock time - time from start to finish of the call. This is all elapsed time including time slices used by other processes and time the process spends blocked (for example if it is waiting for I/O to complete).
User is the amount of CPU time spent in user-mode code (outside the kernel) within the process. This is only actual CPU time used in executing the process. Other processes and time the process spends blocked do not count towards this figure.
Sys is the amount of CPU time spent in the kernel within the process. This means executing CPU time spent in system calls within the kernel, as opposed to library code, which is still running in user-space. Like 'user', this is only CPU time used by the process. See below for a brief description of kernel mode (also known as 'supervisor' mode) and the system call mechanism.
"""
# 3
with open('delete_many_one_05_08_2019.csv') as file, open('result_task_3.csv', 'w') as file_to_save:
data = file.readlines()[1:]
for d in data:
jason = d.split(',')[:14]
jason = str(jason).replace('[', '').replace(']', '').replace('\'', '').replace('""', '"')
jason = jason[1:-1]
data = json.loads(jason)
device_id = data['device_id']
ding_id = data['ding_id']
file_to_save.write(f'{device_id},{ding_id}\n')
# 4
with open('result_task_3.csv', 'r') as file:
result = {}
for line in file.readlines():
line = line.split(',')
key = line[0].strip()
value = line[1].strip()
if key in result:
result[key].append(value)
else:
result[key] = [value]
for k, v in result.items():
with open(f'data/{k}.csv', 'w') as file:
[file.write(f'{x}\n') for x in v]