2

I am trying to write a script in python to loop through a list of lat/long coordinates and send each set through a boundary API call. I want to write each individual API response.

import json
import requests

coords = ['lat1, long1','lat2, long2','lat3, long3']

for x in coords:
    loc={'?contains':'x','&sets':'a_parameter'}
    response = requests.get('http://apicall.com/', params=loc)
    data = response.json()
    print data

I know that this is not the proper way to syntax the 'x' within the API call, but I cannot find documentation of a for loop including a requests API call.

4
  • What is your question? If you want to refer to x variable don't quote it. Are you wishing to group together lat-lng pairs of your list? Commented Aug 8, 2013 at 2:36
  • We need more information, what API, what do you mean by syntax the x ? Commented Aug 8, 2013 at 2:40
  • I am using the Tribune Boundary Service API (boundaries.tribapps.com/api). @Jared The goal is to input the Lat Long and receive back the name of the neighborhood where that coordinate lies. I need to take each Lat Long "pair" and send it through the API call, the single quotes inside the coords list should accomplish this. My issue has been the response received. The API service keeps sending me the same information for each call b/c it is not recognizing the '?contains' parameter I am cycling through. I will try this without the quoted variable and report back. Thanks. Commented Aug 8, 2013 at 17:20
  • 1
    I think you should remove ? and & from '?contains' and '&sets' because they're not needed requests will do this for you, check passing parameters in urls in the docs Commented Sep 9, 2013 at 10:39

1 Answer 1

1

I think you want to do something like this:

for x in coords:
    loc={'?contains' : x , '&sets' : 'a_parameter'}
    ...

This references the x variable, not the string 'x'.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.