-1

I have payload string that i want to modify in a loop but i can not figure out how to convert this '41.86464600000019' and this '-87.80732699999984' in variables to replace them in a loop. inserting f{} did not help

here is the full string

  payload = "{\"showPrice\":\"DISCOUNT\",\"searchRange\":80.467,\"points\":[{\"latitude\":41.86464600000019,\"longitude\":-87.80732699999984}],\"cardNumber\":\"blablabla\",\"_mt926720371\":\"3796524828557652229\"}"
2
  • The syntax is : a=1234 ; st=f"bonjour {a}" ; st -> Out[230]: 'bonjour 1234' ; thus, for you : payload = f"....... {LATITUDE} ......." .
    – Swifty
    Commented Nov 11, 2022 at 22:10
  • What did you try exactly? Please make a minimal reproducible example. For more tips, like how to write a good title, please read How to Ask. If you don't know how to use f-strings, do a tutorial, for example the official one.
    – wjandrea
    Commented Nov 11, 2022 at 22:11

1 Answer 1

0

Quite difficult with strings. You can switch payload to dict.

payload = {'showPrice': 'DISCOUNT',
 'searchRange': 80.467,
 'points': [{'latitude': 41.86464600000019, 'longitude': -87.80732699999984}],
 'cardNumber': 'blablabla',
 '_mt926720371': '3796524828557652229'}

# a set of two (lat, lon) tuples just for the sake of example
coords = [(41.86464600000019, -87.80732699999984), (41.86464600000020, -87.80732699999985)]

for lat, lon in coords:
    payload["points"] = [{"latitude": lat, "longitude": lon}]
    payload = json.dumps(payload)  # if you need to obtain it as a string

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.