-1

I have a post request returning a list: [u'2']

I am trying to extract the number and turn it into and integer but I keep getting either 'float' object not callable or 'int' object not callable.

Here is what I have tried so far:

speed = [u'2']

strSpeed = speed[3]
intSpeed = int(strSpeed)

and

strSpeed = speed[3]
intSpeed = float(strSpeed)

and

strSpeed = speed[3]
intSpeed = int(float(strSpeed))

It seems that I can do:

print float(strSpeed) 

but I can't return it.

Any ideas?

Konig
  • 33
  • 5
  • 1
    How can you do `speed[3]`? `speed` has `len == 1` – Cory Kramer Jun 22 '15 at 19:50
  • well it's odd because when I print the post request I get [u'2']. – Konig Jun 22 '15 at 19:52
  • I think it might be something like [[u'2']] – Konig Jun 22 '15 at 19:53
  • "it might be"? Figure it out, we cannot guess or read your mind. Find exactly what the value of `speed` is – Cory Kramer Jun 22 '15 at 19:55
  • I think you tried too hard to break the question apart from what you're doing and lost the problem in the process. The code there doesn't make any sense... and you don't say what happens when you try to run it :) – Paul Becotte Jun 22 '15 at 19:57
  • Turns out something was messed up in previous code that was causing the post request to turn the list to a string. Noob mistake! hah – Konig Jun 22 '15 at 20:19

3 Answers3

1

You have a list of Unicode strings:

>>> speed
[u'2']

Obtain the first item from the list, it's a Unicode string:

>>> speed[0]
u'2'

Convert this string to an integer:

>>> int(speed[0])
2

Here you are.

dlask
  • 8,776
  • 1
  • 26
  • 30
0

Your speed variable has only a single item, so you can only access index [0]

>>> int(speed[0])
2
>>> speed[0]
'2'

The u is a prefix to declare a unicode string literal. So speed is just a list with a single unicode string.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
0

Not sure exactly what you are trying to do but if you have a list of string items and you want to extract and convert to integters or floats, you could do the following:

stringlist = ["1", "2", "3.2"]

intlistitem = int(stringlist[0])
print(intlistitem)

floatlistitem = float(stringlist[2])
print(floatlistitem)
sw123456
  • 3,339
  • 1
  • 24
  • 42