簡體   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