-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
45 lines (37 loc) · 1.29 KB
/
test.py
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 json
with open("data.json", "r") as file:
data = json.load(file)
# Extract the first two key pairs from each object in the data set
extracted_key_pairs = []
for item in data:
extracted_keys = list(item.keys())[:2] # Get the first two keys
extracted_values = [
item[key] for key in extracted_keys
] # Get the corresponding values
extracted_key_pairs.append((extracted_keys, extracted_values))
# print(extracted_key_pairs)
# Remove the first two key pairs from each object in the data set
for item, (keys, _) in zip(data, extracted_key_pairs):
for key in keys:
del item[key]
# Also remove the last key pair from each object in the data set
for item in data:
del item[list(item.keys())[-1]]
# print(data)
new_data = []
for item in data:
# print(item)
for key in item:
# print(key)
# print(item[key])
split_data = item[key].split()
# print(split_data)
temp_data = [item for item in split_data]
print(temp_data)
new_data = [{str(key): item} for item in temp_data]
print(json.dumps(new_data))
with open("test.json", "w") as file:
json.dump(new_data,file, indent=2)
# Save the updated data set to a new JSON file
with open("updated_data.json", "w") as file:
json.dump(data, file, indent=2)