简体   繁体   中英

Calling in functions in another function

I created a function to read in a csv file and then write some of the data from the csv file into another file. I had to manipulate some of the data in the original csv file before I write it. I will probably have to do that manipulation a lot during the next couple months so I wrote another function to just do that manipulation, but I am having trouble calling in the function in my other function.

this is the function I am trying to call in:

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)

How do I call in this function in another function? They are currently in the same directory. I am used to Matlab and thought it would be a simple call in command but I can not seem to figure it out. Any help will be greatly apprectiated.

Shay

You can import the file (same as you imported sys and math ). If your function is in a file called util.py :

import util
util.convLatLon(37.76)

If the file is in another directory, the directory must be in your PYTHONPATH.

Is:

from <filename> import convLatLon

What you're looking for?

Sounds like you need to import the file.

If your function is defined in file myfile.py and you wan`t to use it from myotherfile.py, you should import myfile.py, like this:

import myfile

and then you can use the function like this:

result = myfile.myfunc(myparms)

If you want to get rid of the myfile prefix, import it like this:

from myfile import myfunc

If you have this function saved in a file called myconv.py

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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