str to float (rounded)

Diez B. Roggisch deets at nospam.web.de
Tue Jun 10 16:30:58 CEST 2008


Nader wrote:

> Hello,
> 
> I have a list of tuple with strin elements. These elements are number,
> but they are save as string. Now I will change the string to number
> which will be rounded. An example will make it more clear.
> 
> t = [('35.757', '-0.239'), ('33.332', '-2.707'), ('33.640', '-2.423')]
> 
> And I will have the next list:
> 
> t = [(35.76, -2.24), (33.33, -2.71), (33.64, -2.42)]
> 
> The elements of tuple are not more as string.
> 
> Would somebody tell me how I can do that?

use 

float("123.45")

to convert a string to a float.

Of course you need to do that on all your elements above by e.g. a
list-comprehension.

Diez



More information about the Python-list mailing list