float from numbers in text file

Stephen Hansen me+list/python at ixokai.io
Mon Jun 21 19:18:20 EDT 2010


On 6/21/10 4:03 PM, davidgp wrote:
> 
> sorry :)

Okay, I should be more specific: include full tracebacks and some real
copied and pasted code :) Don't throw away nice debugging information
Python gave you, feed it to us.

> invalid literal for long() with base 10: '51.9449702'
> this is the error i'm getting when i use long(line)

Yes, "51.9449702" is an invalid literal for long. Long produces
integers: no decimal points.

However:
> and this is the error for float(line)
> invalid literal for float(): not found

Its a perfectly valid literal for float:
>>> float('51.9449702')
51.9449702

So if you're getting that error, you're doing something else that you're
not telling us.

I suspect, somehow (I'd have to see your code to be sure), that your
"line" in the second case doesn't have that number. Try it in the
interactive interpreter. float('51.9449702') works fine. I suspect your
"line", for whatever reason, contains the string "not found", as in:

>>> float('not found')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): not found

-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 487 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20100621/b5c74591/attachment-0001.sig>


More information about the Python-list mailing list