float from numbers in text file

D

davidgp

hello,
i have a text file that contains gps coordinates that i want to load
into my mysql database
the file is basically in this format:
52.2375412
5.1802704

i basically tried this:
lat =0.0
for line in f:
lat = float(line)

but this gives an error.. does anyone know what i should to do?
thanks,
 
S

Stephen Hansen

i basically tried this:
lat =0.0
for line in f:
lat = float(line)

but this gives an error.. does anyone know what i should to do?
thanks,

"An error"?

Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.... print float(line)
...
52.2375412
5.1802704

Always include what the actual error is that you're running into.

--

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


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)

iQEcBAEBAgAGBQJMH+7xAAoJEKcbwptVWx/l05UIALYBfxeq9o0GDxLXDnrs02CZ
9B1qnaxX+k63VuXO5lXwYoftEHMu/nWLt1M6Z5YTMDJnVj70e5wu1k5D+8AEsWf+
DpQk+GMxuiUlJBUWR5uu0g/U9mZJvHftKUkYRccdSdFubRPDyKBakarThs6AoK4y
xJAmXc4NDxusff0Me6N7hew8NWJj/TXz28Cy+yhyAf3+dLz8OT9sPU07sg5ZasZg
dgKBtpBBMElDdahjvbbef82QCl9e3qI12XtxpoDFza4DrrgEwvMag2z4QsRMgfUz
IMw87jfrKCAAep5mWnewDBncdg2A9PleWlXMkDesbTTnEFb7jeO23jb95u3JRZ0=
=3fMm
-----END PGP SIGNATURE-----
 
D

davidgp

i basically tried this:
lat =0.0
for line in f:
  lat = float(line)
but this gives an error.. does anyone know what i should to do?
thanks,

"An error"?

Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.>>> f = open("test.txt", "r")
...     print float(line)
...
52.2375412
5.1802704

Always include what the actual error is that you're running into.

--

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

 signature.asc
< 1KViewDownload

sorry :)

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

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

cheers,
 
S

Stephen Hansen


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: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:
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/


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)

iQEcBAEBAgAGBQJMH/M8AAoJEKcbwptVWx/lbFUH/3bPCuMRlucsAI01sFWlHMv+
mrAidIjlJC1O/GVrGXl6QA5R+2pNuIfbebWit6ZIf2G1spDS2KWf0KYkMbhBqFdV
PEqS+016BNqF2amxaQZdL8eEszvWzWFTZlBwQTS8WV4s+4wOv1IfoD0GvNueUjGj
2UWrhwim5RYgwnPPg3QqaL8mgb4Zf4S3L/7kfgajJvl5+W/adV1eRvNUWhY4T77T
ewAbrn6I039K3O2G1ztE2jolJMcK64BhFuqmGhJqdYbVq5g1i/d98qlDGVyMu0zG
Q/O3bYMB19k3xt1hApaUKfI9WocVdiHvO8OzE8Ah5qJn5SJIzpoAj0nr8Fd03k4=
=IrGu
-----END PGP SIGNATURE-----
 
D

davidgp

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.


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

However:


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:


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/

 signature.asc
< 1KViewDownload

ah, i see :p
float("45.34") or whatever does work fine, but the problem is that i'm
reading it from a text file. so somehow it is not a real string or
whatever..
here's a part of the code:
f = open ('/home/david/out.txt', 'r')

for line in f:
if tel ==6:
buf = line.replace('\n', '')
lat = float(buf)
if tel ==7:
buf = line.replace('\n', '')
lng = float(buf)

basically it goes wrong in the last part where i try to convert the
line to a float..
i'm 100% sure that it's getting a number, not a text string

cheers!
 
G

GMail Felipe

ah, i see :p
float("45.34") or whatever does work fine, but the problem is that i'm
reading it from a text file. so somehow it is not a real string or
whatever..
here's a part of the code:
f = open ('/home/david/out.txt', 'r')

for line in f:
if tel ==6:
buf = line.replace('\n', '')
lat = float(buf)
if tel ==7:
buf = line.replace('\n', '')
lng = float(buf)

basically it goes wrong in the last part where i try to convert the
line to a float..
i'm 100% sure that it's getting a number, not a text string

cheers!

Ok! Just to be sure, execute the following in your <out> file:

egrep -in 'not found' <out>

If it finds something, it will return the line number and what was
found!

Felipe.
 
S

Stephen Hansen

ah, i see :p
float("45.34") or whatever does work fine, but the problem is that i'm
reading it from a text file. so somehow it is not a real string or
whatever..
here's a part of the code:
f = open ('/home/david/out.txt', 'r')

for line in f:
if tel ==6:
buf = line.replace('\n', '')
lat = float(buf)
if tel ==7:
buf = line.replace('\n', '')
lng = float(buf)

basically it goes wrong in the last part where i try to convert the
line to a float..
i'm 100% sure that it's getting a number, not a text string

First: you don't have to strip \n's. Try float("45.43\n")

Second: this code, as is, can't run to show us the problem because
there's this "tel" business and I don't know what it is. But I'll tweak
it a bit and:

f = open ('/Users/ixokai/out.txt', 'r')

tel = 6
for line in f:
if tel == 6:
buf = line.replace('\n', '')
lat = float(buf)
print lat
if tel == 7:
buf = line.replace('\n', '')
lng = float(buf)

And it works fine with a dummy data file I have. So... the problem is
somewhere else. Its either in your data file, or some other logic going
on (maybe something related to this 'tel' thing, I don't know).

Are you sure there's no lines in out.txt which have, say, some text (on
the left end)? Or maybe some blank lines?

Maybe you should send the whole file and the data.txt if you don't see
where the problem is.

--

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


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)

iQEcBAEBAgAGBQJMIAAdAAoJEKcbwptVWx/l9sgH/jt6H4JPL1HqZsC+1t85BwXo
rkQlihZSoTRxTNyVoLGfLET+hYasuOMRjXcrpItRRrQPqac6ZG2GquReLnVepRwS
ZF2qMN0gmfZuITpbyUvIVJ4Wl5uqaqv9jfB3qJTIj4QvgJe4HR07OW+R6bLTZKrw
Rzb9dfV9E7fPO1UUGVIG9kYDl3HMWrDi65teo5yyG7mQenuwbimXnWz5m+uMH3Hx
zNTMz7C65T0l/n3KaaSprMQea+wOZzJlpKdMhy7If5+7eaE1/1Xpj1Wb9S4B+SNh
PJ0pOMHfizOXbVMTJ/8ML7C3onOJAaPQ99ohC88NaW346ly0D0UJAJiWAI23Ois=
=qXtH
-----END PGP SIGNATURE-----
 
D

davidgp

First: you don't have to strip \n's. Try float("45.43\n")

Second: this code, as is, can't run to show us the problem because
there's this "tel" business and I don't know what it is. But I'll tweak
it a bit and:

  f = open ('/Users/ixokai/out.txt', 'r')

  tel = 6
  for line in f:
     if tel == 6:
        buf = line.replace('\n', '')
        lat = float(buf)
        print lat
     if tel == 7:
        buf = line.replace('\n', '')
        lng = float(buf)

And it works fine with a dummy data file I have. So... the problem is
somewhere else. Its either in your data file, or some other logic going
on (maybe something related to this 'tel' thing, I don't know).

Are you sure there's no lines in out.txt which have, say, some text (on
the left end)? Or maybe some blank lines?

Maybe you should send the whole file and the data.txt if you don't see
where the problem is.

--

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

 signature.asc
< 1KViewDownload

i found the error! apparently something was going wrong in the loop..
thanks for the help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top