如何将功能导入其他文件?

时间:2019-08-16 11:07:33

标签: python python-3.x

我正在尝试使用一个文件中另一个文件中的功能,所以我不需要重复代码。

file.py

def getTrain(data):
    trainList = []
    for list in data:
        for train in list['HorarioDetalhe']:
            trainDict = {}
            trainDict['id'] = train['ID']
            trainDict['origin'] = train['EstacaoOrigem']['Nome']
            trainDict['destiny'] = train['EstacaoDestino']['Nome']
            trainDict['operator'] = train['Operador']['Nome']
            trainDict['status'] = train['EstadoComboio']['Nome']
            trainDict['arrivalTime'] = train['HoraChegada']
            trainList.append(trainDict)
        getDelayedCSV(trainList)
        getDeletedCSV(trainList)

我想在另一个文件中使用getDealayedCSV()getDeletedCSV(),因为我想要这两个函数的结果。

1 个答案:

答案 0 :(得分:0)

from myfile import getDealayedCSVfrom myfile import getDeletedCSV

如果文件名为myfile.py,则只需编写myfile。然后,您可以像调用getDealayedCSV(trainList)一样正常调用函数