繁体   English   中英

在另一个函数中调用函数

[英]Calling in functions in another function

我创建了一个函数来读取csv文件,然后将csv文件中的某些数据写入另一个文件。 在写入之前,我不得不处理原始csv文件中的某些数据。 在接下来的几个月中,我可能将不得不做大量的操作,因此我编写了另一个函数来执行该操作,但是我在另一个函数中调用该函数时遇到了麻烦。

这是我要调用的函数:

import sys
import math

def convLatLon(measurement): # should be in '##.####' format    
    tpoint=float(measurement)    
    point_deg=math.floor(measurement) # find the degree for lat and lon 
    dpoint=tpoint-point_deg #subtract the lat value from just the degs to get the                   decimal fraction
    pointmin=dpoint * 60 # find the degree mins

    npoint= str(point_deg) + str(pointmin)        
    print(npoint)

如何在另一个函数中调用此函数? 它们当前在同一目录中。 我习惯了Matlab,并认为这只是命令中的简单调用,但我似乎无法弄清楚。 任何帮助将不胜感激。

吉文

您可以导入文件(与导入sysmath )。 如果您的函数在一个名为util.py的文件中:

import util
util.convLatLon(37.76)

如果文件在另一个目录中,则该目录必须在您的PYTHONPATH中。

方法是:

from <filename> import convLatLon

您在找什么?

听起来您需要导入文件。

如果在文件myfile.py中定义了函数,并且希望从myotherfile.py中使用它,则应导入myfile.py,如下所示:

import myfile

然后您可以使用如下功能:

result = myfile.myfunc(myparms)

如果要摆脱myfile前缀,请按如下所示导入它:

from myfile import myfunc

如果您将此功能保存在名为myconv.py的文件中

from myconv.py import convLatLon
convLatLon("12.3456")

暂无
暂无

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

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