forked from ssciwr/Python-best-practices-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplexity2.py
36 lines (28 loc) · 1.01 KB
/
complexity2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import numpy as np
import pandas as pd
def read_in(filedir, filename, data_type, myheader=False):
input_handler = {"numpy": read_numpy, "pandas": read_pandas}
reading_method = input_handler.get(data_type)
if not reading_method:
print("Data type not found!")
exit()
data = reading_method(filedir, filename, myheader)
return data
def read_numpy(filedir, filename, myheader):
name = "{}{}".format(filedir, filename)
print("Reading from file {} - numpy".format(name))
if myheader:
data = np.loadtxt(name, skiprows=1)
else:
data = np.loadtxt(name, skiprows=0)
data = data.T
return data
def read_pandas(filedir, filename, myheader):
name = "{}{}".format(filedir, filename)
print("Reading from file {} - pandas".format(name))
data = pd.read_csv(name, sep=r"\s+")
return data
if __name__ == "__main__":
read_in("./data/", "efield.t", "numpy", myheader=True)
read_in("./data/", "npop.t", "pandas")
read_in("./data/", "npop.t", "foo")