python处理json脚本
import json
import os
path = '/home/zqf/bridge/test'
original_name = os.listdir(path)
print(original_name)
for i in original_name:
filename=os.path.join(path,i)
bret = filename.endswith(".json")
if bret:
savename = os.path.splitext(filename)[0]
print(savename)
savename=savename+".txt"
print(filename)
print(savename)
savename=os.path.join(path,savename)
#with open(filename) as data_file:
#file = json.load(data_file)
f = open(filename, "r")
file = json.load(f)
print(file['labels'])
for item in file['labels']:
x1 = item["x1"]
print(x1)
y1 = item["y1"]
print(y1)
x2 = item["x2"]
print(x2)
y2 = item["y2"]
print(y2)
width = item["size"]["width"]
print(width)
height = item["size"]["height"]
print(height)
a1=float(x1)/float(width)
b1=float(y1)/float(height)
a2=float(x2)/float(width)
b2=float(y2)/float(height)
retstr='0 '+'%.6f'%a1+' '+'%.6f'%b1+' '+'%.6f'%a2+' '+'%.6f'%b2+'\n'
with open(savename, 'a') as f:
f.write(retstr)