有没有一种方法可以使用python中API的数据为单独的输入循环函数?

2024-06-01 01:16:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我可能把标题写错了。我想知道是否有可能为一个特定的输入重复一个函数。显示代码将更容易解释。 代码如下:

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,是否有一种方法可以让bot自动搜索输入的公交线路的预期发车时间,而不必手动复制每条公交线路的代码?例如,如果我为公交站点“CU2”输入公交线路“8”,bot将检查该站点的API,找到公交线路“8”,并打印预期的发车时间

如果需要,我可以提供更多的细节


Tags: to代码importjsonwhichtimelineresp
3条回答

我推断您不只是重用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']

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

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 = 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,因为找不到元素,在这里,我们将捕获它并将您的错误与用户提供的输入一起输出

与其打印输出,我还建议返回您要查找的信息,然后在主循环中打印输出。这样你的功能就可以重用了。另外,当捕捉到KeyError时,请考虑使用raise(返回自定义错误)或return,而不是像这里那样打印出来

相关问题 更多 >