forked from ssciwr/Python-best-practices-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplexity4.py
36 lines (28 loc) · 942 Bytes
/
complexity4.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()
name = "{}{}".format(filedir, filename)
data = reading_method(name, myheader)
return data
def read_numpy(name, myheader):
print("Reading from file {} - numpy".format(name))
if myheader:
skip = 1
else:
skip = 0
data = np.loadtxt(name, skiprows=skip)
data = data.T
return data
def read_pandas(name, *kwargs):
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")