使用从以下链接获得的代码:http://python-overpy.readthedocs.org/en/latest/example.html我想输入我已经获得的变量而不是直接输入它们,如下所示:
import overpy
api = overpy.Overpass()
result = api.query("node(min_lat,min_lon,max_lat,max_lon);out;")
len(result.nodes)
变量min_lat等是float类型。这是我得到的错误:
overpy.exception.OverpassBadRequest: Error: line 1: parse error: Unknown query clause
Error: line 1: parse error: ')' expected - 'min_lat' found.
Error: line 1: parse error: An empty query is not allowed
Error: line 1: parse error: Unknown type ";"
Error: line 1: parse error: An empty query is not allowed
任何帮助都非常感谢,因为我对这一切感到非常困难和新的感谢!
答案 0 :(得分:1)
您实际上是将查询node(min_lat,min_lon,max_lat,max_lon);out;
发送到Overpass API,min_lat
等不变。
要填充变量,您可以使用字符串替换:
"node(%s, %s, %s, %s);out;" % ( min_lat, min_lon, max_lat, max_lon )
%s
将替换为您在括号中提供的变量。
import overpy
api = overpy.Overpass()
min_lat = 50.745
min_lon = 7.17
max_lat = 50.75
max_lon = 7.18
query = "node(%s, %s, %s, %s);out;" % ( min_lat, min_lon, max_lat, max_lon )
result = api.query(query)
print query
print len(result.nodes)