我创建了一个函数来读取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,并认为这将是一个简单的命令调用,但我似乎无法弄明白。任何帮助都会非常感激。
谢伊
答案 0 :(得分:3)
您可以导入文件(与导入sys
和math
相同)。如果您的函数位于名为util.py
的文件中:
import util
util.convLatLon(37.76)
如果文件位于另一个目录中,则该目录必须位于PYTHONPATH中。
答案 1 :(得分:0)
时:
from <filename> import convLatLon
您在寻找什么?
答案 2 :(得分:0)
听起来你需要导入文件。
如果您的函数是在文件myfile.py中定义的,并且您不想在myotherfile.py中使用它,则应该导入myfile.py,如下所示:
import myfile
然后你可以使用这样的函数:
result = myfile.myfunc(myparms)
如果你想摆脱myfile前缀,请像这样导入:
from myfile import myfunc
答案 3 :(得分:0)
如果您将此功能保存在名为myconv.py
from myconv.py import convLatLon
convLatLon("12.3456")