繁体   English   中英

有没有办法使用 python 中的 API 中的数据循环 function 以获得单独的输入?

[英]Is there a way to loop a function for a separate input using data from an API in python?

我可能用错了标题。 我想知道是否可以为特定输入重复 function。 显示代码会更容易解释。 这是代码:

from urllib.request import urlopen
import json

def askbot(bus_stop):

    if bus_stop == "CU2":
        url = urlopen("https://transportapi.com/v3/uk/bus/stop/43001053801/live.json?app_id=&app_key=&group=route&nextbuses=yes")
        data = json.loads(url.read().decode())
        json_str=json.dumps(data)
        resp=json.loads(json_str)  
        which_line = input("Which bus line would you like to know? ")
        if which_line == "10":
           print("Here is the current expected departure time at bus stop " + bus_stop + " for bus line " + resp['departures']['10'][0]['line_name'] + " heading to " + resp['departures']['10'][0]['direction'])
           print("Expected departure time: " + resp['departures']['10'][0]['expected_departure_time'])
           print("The next bus at bus stop " + bus_stop + "," + " for bus line " + resp['departures']['10'][1]['line_name'] + " will be heading to " + resp['departures']['10'][1]['direction'])
        elif which_line == "8":
            print
        else:
            print("That is not a valid line!")
    else:
        print("That bus stop does not exist!")
which_stop = input("Which bus stop timetable would you like to know? ")

askbot(which_stop)

我的问题是,它在哪里询问用户which_line ,有没有办法让机器人自动搜索输入的公交线路的预期出发时间,而无需手动复制每条公交线路的代码? 例如,如果我为公交车站“CU2”输入公交线路“8”,机器人将检查该站点的 API 并找到公交线路“8”并打印预计出发时间。

如果需要,我可以提供更多详细信息。

假设索引与您在上面的操作方式相似,可能会沿着这些思路:

which_line = input("Which bus line would you like to know? ")

try:
    print("Here is the current expected departure time at bus stop " + bus_stop + " for bus line " +
        resp['departures'][which_line][0]['line_name'] + " heading to " + resp['departures'][which_line][0]['direction'])
    print("Expected departure time: " +
        resp['departures'][which_line][0]['expected_departure_time'])
    print("The next bus at bus stop " + bus_stop + "," + " for bus line " +
        resp['departures'][which_line][1]['line_name'] + " will be heading to " + resp['departures'][which_line][1]['direction'])
# If Line was not found
except KeyError as err:
    print("%s is not a valid line: %s" % (which_line, err))

这样,您就不必为所有行编写 if 语句,如果您输入的内容不在您的 dict 中,您应该得到一个 KeyError,因为找不到该元素,在这里我们抓住它,output 您的错误与用户提供的输入一起.

而不是打印出 output 我还建议返回您正在寻找的信息,然后在主循环中打印出来。 这样您的 function 就更可重复使用。 此外,当捕获 KeyError 时,请考虑使用raise (返回自定义错误)或return而不是像我在这里所做的那样打印出来。

您可以将行号作为变量插入。

which_line = input("Which bus line would you like to know? ")
print("Here is the current expected departure time at bus stop " + bus_stop + " for bus line " + resp['departures'][which_line][0]['line_name']

请注意

resp['departures']['10'][0]['line_name']

变成

resp['departures'][which_line][0]['line_name']

这也取决于您使用的 API。 如果它可以接受行号,则可以在用户输入行号后调用 API 并仅检索该行的数据。

我推断您不只是重用which_line的原因是因为您不想接受所有值。 如果是这种情况,我建议您创建一个可接受值的列表并将其作为您的条件。

existing_lines = [10, 8, 7, 12, 21]
if which_line in existing_lines:
    print("Here is the current expected departure time at bus stop " + bus_stop + " for bus line " + resp['departures'][which_line][0]['line_name']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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