Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中调用以list为变量的API函数?_Python_List_Loops - Fatal编程技术网

如何在Python中调用以list为变量的API函数?

如何在Python中调用以list为变量的API函数?,python,list,loops,Python,List,Loops,我有一个python脚本,它通过一个overpassAPI成功地从OSM中提取POI(兴趣点)数据。有三个输入变量符合请求:“国家/地区代码”、“主类型”和“值类型” overpass_query = f""" [out:json]; area["ISO3166-1"="{country_code}"][admin_level=2]; ( node["{master_type}"="{valu

我有一个python脚本,它通过一个overpassAPI成功地从OSM中提取POI(兴趣点)数据。有三个输入变量符合请求:“国家/地区代码”、“主类型”和“值类型”

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()
虽然“国家/地区代码”和“主类型”只有一个值,但有多个“值类型”。为此,我创建了一个名为value\u type\u list的列表

value_type_list = ['restaurant','fuel','casino']
我想为value\u type变量中的每个value\u type\u列表循环此脚本。如何做到这一点

多谢各位

简单地这样做:

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)

输出:

    [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;
...

感谢您的快速回复,塞巴斯蒂安。但是,我在运行函数时遇到了一个错误-可能是因为列表中并非所有项都是“可调用的”。关于如何跳转到下一项并忽略错误,有什么提示吗?