0

I am working on a code and there I get data in the form of :

k=[{'item_id': '43381702', 'length': {'_exp': -2, '_is_special': False, '_int': '0', '_sign': 0}, 'height': {'_exp': -2, '_is_special': False, '_int': '0', '_sign': 0}, 'width': {'_exp': -2, '_is_special': False, '_int': '0', '_sign': 0}, 'unitweight': {'_exp': -3, '_is_special': False, '_int': '3000', '_sign': 0}, 'quantity': 1}]

And It is expected that when I write

float(k[0]['unitweight'])

It should be converted into a float number . But It is giving

TypeError: float() argument must be a string or a number

as expected. I am confused as the data is of the same form as is implemented inside python Decimal Module. Is there any way (one-liner) By which I can convert the data in float ?

This link has the same question probably . Check out this also .

Community
  • 1
  • 1
tusharmakkar08
  • 706
  • 1
  • 12
  • 32
  • `is_special` is not an attribute of `Decimal` – jamylak May 31 '13 at 06:37
  • I think It is the attribute of a number. Check out [tip](http://www.itmaybeahack.com/homepage/books/nonprog/html/p13_modules/p13_c03_decimal.html#rounding-known-as-quantization) about attribue error and this [link](http://mail.python.org/pipermail/python-checkins/2007-November/063656.html) also which shows the modification in python's code . – tusharmakkar08 May 31 '13 at 06:42

1 Answers1

2

Taking this:

d = {'_exp': -3, '_is_special': False, '_int': '3000', '_sign': 0}

for:

k[0]['unitweight']

You could do something like this:

import math  
math.copysign(int(d['_int']) ** d['_exp'], d['_sign'] * -1)

Result:

3.7037037037037036e-11

No idea what '_is_special' means.

Mike Müller
  • 82,630
  • 20
  • 166
  • 161