I wrote a function to open a txt file and returns me three numpy array. However, I get this error all the time, which I do not have any idea why?
TypeError: load_data_from_file() missing 1 required positional argument: 'filename'
here is my code:
def load_data_from_file(self, filename):
# logging.info("Entered load_data_from_file")
with open(filename, 'r') as data_file:
wavelengths = []
spectra = []
delays = []
for num, line in enumerate(data_file):
if num == 0:
# Get the 1st line, drop the 1st element, and convert it
# to a float array.
delays = array([float(stri) for stri in line.split()[1:]])
else:
data = [float(stri) for stri in line.split()]
# The first column contains wavelengths.
wavelengths.append(data[0])
# All other columns contain intensity at that wavelength
# vs time.
spectra.append(array(data[1:]))
logging.info("Data loaded from file has sizes (%dx%d)" % (delays.size, len(wavelengths)))
return delays, array(wavelengths), vstack(spectra).Tenter code here
I appriciate any comments on this.
CodePudding user response:
The error you are referring is when you call the function load_data_from_file().It seems that you are not passing the filename correctly in the parameters. The correct way to call it should be something like:
output= load_data_from_file(myfile.txt)
Also, I'm not sure, but if you are passing parameter "self" should indicate that you are using class? Please upload your full code here so we can have a better understanding of the issue.
CodePudding user response:
According to the question comments, the problem seems to be that this is actually a class method (a function defined inside a class). Because of this, you have to use your function as a method of a class instance:
my_instance = YourClass(...)
my_instance.load_data_from_file(filename)
This way, your self
and filename
arguments are correctly used.