Converting Unicode Data to Python Data

I have the following list:

list = [u'0', u'FF', u'7', u'0', u'FF', u'FFF', u'FFF']

      

and i need to use elements as integer or float, but when i try convevrting i get the following error:

>>> float(list[1])
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
ValueError: could not convert string to float: FF

      

Is there a way to solve this?

+3


source to share


1 answer


You cannot directly convert these hex values ​​to float

, instead you can convert to int specifying the correct base using the function int()

:

>>> l = [u'0', u'FF', u'7', u'0', u'FF', u'FFF', u'FFF']
>>> [int(i,16) for i in l]
[0, 255, 7, 0, 255, 4095, 4095]

      



Or use float

on int

values:

>>> [float(int(i,16)) for i in l]
[0.0, 255.0, 7.0, 0.0, 255.0, 4095.0, 4095.0]

      

+3


source







All Articles