如何在JSON中保存浮点值?

时间:2018-09-20 10:52:16

标签: json python-3.x

在我的脚本中,我将python 2D列表(均为浮点值)保存在json文件中。当我在机器上运行脚本时,保存的json文件包含正确的数据。但是,当我在xampp服务器中运行python脚本时,json文件仅包含整数值。

我检查了代码,但未发现问题。并且相同的代码输出两个不同的json文件。

当我在机器上运行脚本时;

 {"arr": [[5.7, 7.14, 5.72, 10.96, 15.82, 10.96, 15.78, 7.08], [5.76, 7.2, 15.72, 7.14, 15.76, 10.88, 5.8, 10.9], [4.98, 1.46, 4.98, 11.7, 14.38, 11.7, 14.44, 15.2, 29.14, 15.22, 29.12, 5.7, 21.42, 5.68, 21.34, 1.44], [17.52, 8.76, 23.18, 8.8, 23.16, 15.14, 17.46, 15.1], [21.4, 6.14, 28.74, 6.08, 28.78, 7.54, 21.44, 7.56], [5.02, 6.4, 15.82, 6.36, 15.88, 1.5, 21.28, 1.5, 21.36, 7.62, 29.06, 7.64, 29.06, 15.12, 23.28, 15.14, 23.24, 8.72, 17.48, 8.7, 17.4, 15.12, 14.52, 15.14, 14.46, 11.64, 5.06, 11.64], [5.02, 1.56, 15.72, 1.5, 15.76, 6.28, 5.06, 6.28]]}

当我在xampp服务器中运行脚本时;

{"arr": [[5, 7, 5, 10, 15, 10, 15, 7], [5, 7, 15, 7, 15, 10, 5, 10], [4, 1, 4, 11, 14, 11, 14, 15, 29, 15, 29, 5, 21, 5, 21, 1], [17, 8, 23, 8, 23, 15, 17, 15], [21, 6, 28, 6, 28, 7, 21, 7], [5, 6, 15, 6, 15, 1, 21, 1, 21, 7, 29, 7, 29, 15, 23, 15, 23, 8, 17, 8, 17, 15, 14, 15, 14, 11, 5, 11], [5, 1, 15, 1, 15, 6, 5, 6]]}    

这是我的代码。

#!/usr/bin/env python
import sys;


import cv2
import codecs, json
dataset=[]
image = cv2.imread("C:/xampp/htdocs/"+sys.argv[1])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (3, 3), 0)
edged = cv2.Canny(gray, 10, 250)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (20, 20))
closed = cv2.morphologyEx(edged, cv2.MORPH_CLOSE, kernel)
cv2.waitKey(0)
(_, cnts, _) = cv2.findContours(closed.copy(), cv2.RETR_CCOMP, 
cv2.CHAIN_APPROX_SIMPLE)
total = 0
lengths = 0;
i = 0
for c in cnts:
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.02 * peri, True)
    converted_cnts_rooms = approx.squeeze()

    dataset.append([])
    for iterate in converted_cnts_rooms:
        x1, y1 = iterate.ravel()
        dataset[i].append(x1/50)
        dataset[i].append(y1/50)
    total += 1
    i += 1

cv2.waitKey(0)
converted_cnts = approx.squeeze()
for iterate in converted_cnts:
    x1,y1 = iterate.ravel()

data = {}
data.update({"arr":dataset})

with open('dataset.json', 'w') as outfile:
json.dump(data, outfile)

感谢您的帮助... !!!

0 个答案:

没有答案