-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson数据处理.py
83 lines (40 loc) · 1.01 KB
/
json数据处理.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
# coding: utf-8
处理json数据
并且将文件名重写
# In[2]:
import json
# In[3]:
file = open("AgriculturalDisease_trainingset\AgriculturalDisease_train_annotations.json")
# In[4]:
#加载json文件
data = json.load(file)
# In[5]:
#list
type(data)
# In[6]:
#dict
type(data[0])
# In[7]:
#int
type(data[0]['disease_class'])
# In[8]:
#str
type(data[0]["image_id"])
# In[9]:
#'1_62fd8bf4d53a1b94fbac16738406f10b.jpg'
str(data[0]['disease_class'])+"_"+data[0]["image_id"]
# In[11]:
#文件夹下的文件列表
import os
# In[12]:
old_name = os.listdir(path = "AgriculturalDisease_trainingset\images")
# In[18]:
#注意rename的时候要写上总的路径,不然会出错
path = "AgriculturalDisease_trainingset\images"
for i in range(len(data)):
new_name = str(data[i]["disease_class"])+"_"+data[i]["image_id"]
new_name = os.path.join(path, new_name)
old_name[i] = os.path.join(path, old_name[i])
os.rename(old_name[i], new_name)
print(new_name)