简体   繁体   中英

How to call an API function in Python with list as variable?

I have a python script that successfully pulls POI (points of interest) data from OSM via an overpass-API. There are three input variables to comply with the request: 'country_code', 'master_type' and 'value_type' .

overpass_query = f"""
[out:json];

area["ISO3166-1"="{country_code}"][admin_level=2];

( node["{master_type}"="{value_type}"](area);
  way["{master_type}"="{value_type}"](area);
  rel["{master_type}"="{value_type}"](area);
  
);
out center;
"""

response = requests.get(overpass_url, 
                        params={'data': overpass_query})
data = response.json()

While I have single values for 'country_code' and 'master_type', there are multiple 'value_type'. For this, I created a list called value_type_list

value_type_list = ['restaurant','fuel','casino']

I would like to LOOP this script for every value_type_list within the value_type variable. How can this be done?

Thank you very much!

Simply doing so :

country_code = 1
master_type = "Some value"

for value_type in ['restaurant','fuel','casino'] :
    overpass_query = f"""
    [out:json];
    area["ISO3166-1"="{country_code}"][admin_level=2];
    ( node["{master_type}"="{value_type}"](area);
      way["{master_type}"="{value_type}"](area);
      rel["{master_type}"="{value_type}"](area);
    );
    out center;
    """
    print(overpass_query)

Output:

    [out:json];
    area["ISO3166-1"="1"][admin_level=2];
    ( node["Some value"="restaurant"](area);
      way["Some value"="restaurant"](area);
      rel["Some value"="restaurant"](area);
    );
    out center;
    

    [out:json];
    area["ISO3166-1"="1"][admin_level=2];
    ( node["Some value"="fuel"](area);
      way["Some value"="fuel"](area);
      rel["Some value"="fuel"](area);
    );
    out center;
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM