Reputation: 11
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
Upvotes: 1
Views: 212
Reputation: 304463
If you have this function saved in a file called myconv.py
from myconv.py import convLatLon
convLatLon("12.3456")
Upvotes: 0
Reputation: 5950
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
Upvotes: 0
Reputation: 3838
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.
Upvotes: 3