[Tutor] converting strings to float: strange case

C Smith illusiontechniques at gmail.com
Mon Apr 28 17:26:41 CEST 2014


.split() will split things based on whitespace or newlines. Do you know
that your file is only going to contain things that should convert to
floats? If you post your entire code, the error you have included will be
more helpful as it points to a certain line. The last line in your code has
(stri) should be (str).


On Mon, Apr 28, 2014 at 11:13 AM, Gabriele Brambilla <
gb.gabrielebrambilla at gmail.com> wrote:

> Hi,
>
> I'm trying to convert a string to a float. It seems a basic thing but I
> don't know why I'm getting this erroris
>
> Traceback (most recent call last):
>   File "phresREADER.py", line 27, in <module>
>     tra = float(stri)
> ValueError: could not convert string to float:
>
> My file has this line
>
> 5.50000e+000 5.50000e+001 5.50000e+002 5.50000e+003
>
> my code is:
>
> my_line = f.readline()
> avg_energySTR = [str(i) for i in my_line.split(' ')]
> for stri in avg_energySTR:
>         tra = float(stri)
>
> do you have any idea?
>
> thanks
>
> Gabriele
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140428/a820003b/attachment.html>


More information about the Tutor mailing list