-1

I have such url:

http://0.0.0.0:5000/test?id=00000&coords=[55.530974,37.522081]

And there is some part of the code:

id = request.args.get('id')
 >'00000'

coords = request.args.get('coords')
> '[55.530974,37.522081]'

I need to get coords not like a string, but like a list [55.530974,37.522081]

Is there solution to convert it? Or make in another way?

Thanks a lot

davidism
  • 121,510
  • 29
  • 395
  • 339

4 Answers4

0

Try:

>>> [float(n) for n in coords[1:-1].split(",")]
[55.530974, 37.522081]

Slice from [1:-1] to ignore the []

not_speshal
  • 22,093
  • 2
  • 15
  • 30
0

The server receives the coords from a url, outside of the python runtime. Because of this, it will always come in as a string. The only was is to parse the string into an array of floats. You can either:

  • Split the coords query parameter into two different ones, individually parse them as floats and then assemble them into an array
  • Create a function that parses the array

I would recommend the first one; it is very common to send coordinates like this: ?long=1.11&lat=2.22

Simon Martineau
  • 210
  • 2
  • 8
0

Do some string manipulations like strip() and split() and convert it into a list.

coords = '[55.530974, 37.522081]'

coords = coords.strip('[')
coords = coords.strip(']')
l = list(map(float, coords.split(',')))
print(l)
[55.530974, 37.522081]
Ram
  • 4,724
  • 2
  • 14
  • 22
0

If you do not have the possibility to change how the http request is made, and you are sure that the passed coordinates are always separated by a comma and you have curly brackets at the ends, then you can convert the string to a list in the following way

coords = request.args.get("coords").strip()[1:-1].split(",")
Matteo Pasini
  • 1,787
  • 3
  • 13
  • 26