Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98454

Re: Converting a string into a float that includes the negative

From Pete Dowdell <contact@stridebird.com>
Newsgroups comp.lang.python
Subject Re: Converting a string into a float that includes the negative
Date 2015-11-08 09:39 +0700
Message-ID <mailman.129.1446996900.16136.python-list@python.org> (permalink)
References <2ced12ff-ae0c-4e86-ac38-357558251e51@googlegroups.com>

Show all headers | View raw


On 08/11/15 09:11, phamtony33@gmail.com wrote:
> I am having issue with converting the string into a float because there is a negative, so only end up with "ValueError: invalid literal for float(): 81.4]"81.4]

The error is right there in the exception: you are trying to cast 
'81.4]' - that's a square bracket in the string.
You are also  removing the negative sign from your number before casting 
with your slice

this will fix it - change line:
long1 = long[1:]
to:
long1 = long[:-1]

test:
 >>> TXT="[41.3, -81.4]\t6\t2011-08-28 19:02:28\tyay. little league 
world series!"
 >>> float( TXT.split('\t')[0].split(', ')[1][:-1] )
-81.4

pd

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Converting a string into a float that includes the negative phamtony33@gmail.com - 2015-11-07 18:11 -0800
  Re: Converting a string into a float that includes the negative MRAB <python@mrabarnett.plus.com> - 2015-11-08 02:40 +0000
    Re: Converting a string into a float that includes the negative phamtony33@gmail.com - 2015-11-07 18:44 -0800
  Re: Converting a string into a float that includes the negative Pete Dowdell <contact@stridebird.com> - 2015-11-08 09:39 +0700

csiph-web