json。load将python中的十进制点转换为“e”

时间:2021-12-19 06:04:09

Here is a response I am getting from server

这是我从服务器得到的响应

[{"type":"bid","price":0.00000026,"amount":737.15054457,"tid":200001915,"timestamp":1516036570}]

I am trying to parse this string into JSON using

我正在尝试使用JSON解析这个字符串

json_data = json.loads (req.text)

However when I try to read the 'price' using json_data[0]['price'] the output is 2.6e-07

但是,当我尝试使用json_data[0]['price']读取“price”时,输出是2.6e-07

I tried parsing data as json_data = json.loads (req.text, parse_float=Decimal) but still no difference.

我尝试将数据解析为json_data = json。负载(点播。文本,parse_float=Decimal,但仍然没有区别。

2 个解决方案

#1


1  

This is the way python shows floats

这就是python显示浮动的方式

price = 0.00000026
print(price)

outputs: 2.6e-07 you can print it this way if you want to see it normal:

输出:2.6e-07如果你想看到正常,可以这样打印:

print('{0:.8f}'.format(price))

ouputs: 0.00000026

输出:0.00000026

#2


0  

Your value is parsed as a Decimal, it's just shown in an exponential form because it's more compact:

你的值被解析为小数,它以指数形式显示因为它更紧凑:

>>> x = json.loads('{"a":0.00000000000000026}', parse_float=decimal.Decimal)
>>> repr(x)
"{'a': Decimal('2.6E-16')}"

You can see that the precision is preserved, though, unlike with a float:

你可以看到,它的精度是被保留的,但与浮点数不同:

>>> x['a'] + 1
Decimal('1.00000000000000026')
>>> 1 + 2.6e-16
1.0000000000000002

So everything works as expected.

所以一切都按预期进行。

#1


1  

This is the way python shows floats

这就是python显示浮动的方式

price = 0.00000026
print(price)

outputs: 2.6e-07 you can print it this way if you want to see it normal:

输出:2.6e-07如果你想看到正常,可以这样打印:

print('{0:.8f}'.format(price))

ouputs: 0.00000026

输出:0.00000026

#2


0  

Your value is parsed as a Decimal, it's just shown in an exponential form because it's more compact:

你的值被解析为小数,它以指数形式显示因为它更紧凑:

>>> x = json.loads('{"a":0.00000000000000026}', parse_float=decimal.Decimal)
>>> repr(x)
"{'a': Decimal('2.6E-16')}"

You can see that the precision is preserved, though, unlike with a float:

你可以看到,它的精度是被保留的,但与浮点数不同:

>>> x['a'] + 1
Decimal('1.00000000000000026')
>>> 1 + 2.6e-16
1.0000000000000002

So everything works as expected.

所以一切都按预期进行。