I am new to python and programming. I have tried searching for this, but have not been able to find an answer.
I am looking for best way to take a data file append it to an array, and use it in multiple python modules. As a 1. semester science student, I know I will be using this many times during my education, so I would like some help, to find a better way to do this.
Here is an example from my first semester Group project. Where we were asked to simulate a speaker using python to help as with different tasks. We were given impedans reading to use in a data file. The data file has 3 collumns. Frequenzy, real impedance, imaginary impedance.
In the first file lets call it import_define.py . We have the following code, importing from the datafile called data.
infile = open('data.dat', 'r')
infile.readline()
freq_list = []
real_list = []
imag_list = []
for line in infile:
split = line.split()
freq_list.append(float(split[0][:-1]))
real_list.append(float(split[1][:-1]))
imag_list.append(float(split[2]))
infile.close()
import numpy as np
freqenzy = np.asarray(freq_list)
real_imp = np.asarray(real_list)
imag_imp = np.asarray(imag_list)
Then we have used the three arrays for different calculations within this module.
Now we want to use these arrays in a different module plot.py, where we want to plot some data from these 3 arrays, and from different data calculated in import_define.py
What we have done is to define 3 functions in import_define.py that return the arrays, like so:
def freq_func():
return frequenzy
def real_imp_func():
return real_imp
def imag_imp_func():
return imag_imp
In the file plot.py, we did the following to import and use the arrays in this new module:
import import_define as imd
freq = imd.freq_func()
real = imd.real_imp_func()
imag = imd.imag_imp_func()
So what I would like to ask if there is a better or more correct way to do this, when using python.
freqenzy,real_impandimag_imp.