Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #98414 > unrolled thread
| Started by | phamtony33@gmail.com |
|---|---|
| First post | 2015-11-07 18:11 -0800 |
| Last post | 2015-11-08 09:39 +0700 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.python
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
| From | phamtony33@gmail.com |
|---|---|
| Date | 2015-11-07 18:11 -0800 |
| Subject | Converting a string into a float that includes the negative |
| Message-ID | <2ced12ff-ae0c-4e86-ac38-357558251e51@googlegroups.com> |
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]"
def contains_words(word,msg):
if word in msg:
return true
else:
return false
def get_tweet(tweet_line):
part_list = tweet_line.split('\t')
tweet = part_list[3]
return tweet
def get_longitude(tweet_line):
part_list = tweet_line.split('\t')
gps = part_list[0]
gps_list = gps.split(', ')
long = gps_list[1]
long1 = long[1:]
longitude = float(long1)
return longitude
a = get_longitude("[41.3, -81.4]\t6\t2011-08-28 19:02:28\tyay. little league world series!")
print a
the answer should be
get_longitude("[41.3, -81.4]\t6\t2011-08-28 19:02:28\tyay. little league world series!") → -81.4
[toc] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2015-11-08 02:40 +0000 |
| Message-ID | <mailman.116.1446950434.16136.python-list@python.org> |
| In reply to | #98414 |
On 2015-11-08 02: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]"
>
> def contains_words(word,msg):
> if word in msg:
> return true
> else:
> return false
>
> def get_tweet(tweet_line):
> part_list = tweet_line.split('\t')
> tweet = part_list[3]
> return tweet
>
> def get_longitude(tweet_line):
> part_list = tweet_line.split('\t')
> gps = part_list[0]
> gps_list = gps.split(', ')
> long = gps_list[1]
> long1 = long[1:]
> longitude = float(long1)
> return longitude
>
> a = get_longitude("[41.3, -81.4]\t6\t2011-08-28 19:02:28\tyay. little league world series!")
> print a
>
> the answer should be
> get_longitude("[41.3, -81.4]\t6\t2011-08-28 19:02:28\tyay. little league world series!") → -81.4
>
You're slicing the wrong end off 'long'.
You have '-81.4]'. You want '-81.4'. You should be doing long[ : -1].
[toc] | [prev] | [next] | [standalone]
| From | phamtony33@gmail.com |
|---|---|
| Date | 2015-11-07 18:44 -0800 |
| Message-ID | <9c6f5cf7-bb48-4014-8e4b-ab653c56f24c@googlegroups.com> |
| In reply to | #98415 |
On Saturday, November 7, 2015 at 9:40:49 PM UTC-5, MRAB wrote:
> On 2015-11-08 02: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]"
> >
> > def contains_words(word,msg):
> > if word in msg:
> > return true
> > else:
> > return false
> >
> > def get_tweet(tweet_line):
> > part_list = tweet_line.split('\t')
> > tweet = part_list[3]
> > return tweet
> >
> > def get_longitude(tweet_line):
> > part_list = tweet_line.split('\t')
> > gps = part_list[0]
> > gps_list = gps.split(', ')
> > long = gps_list[1]
> > long1 = long[1:]
> > longitude = float(long1)
> > return longitude
> >
> > a = get_longitude("[41.3, -81.4]\t6\t2011-08-28 19:02:28\tyay. little league world series!")
> > print a
> >
> > the answer should be
> > get_longitude("[41.3, -81.4]\t6\t2011-08-28 19:02:28\tyay. little league world series!") → -81.4
> >
> You're slicing the wrong end off 'long'.
>
> You have '-81.4]'. You want '-81.4'. You should be doing long[ : -1].
Ah! Thank you so much!
[toc] | [prev] | [next] | [standalone]
| From | Pete Dowdell <contact@stridebird.com> |
|---|---|
| Date | 2015-11-08 09:39 +0700 |
| Message-ID | <mailman.129.1446996900.16136.python-list@python.org> |
| In reply to | #98414 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web