def read_data(self, fName): fName += "_optYjunc" self.x = np.read(fName + "_branching_x.npy") self.z = np.read(fName + "_branching_z.npy") self.interpolated_field = np.read(fName + "_interp_field.npy") self.ideal_index_distribution = np.read( fName + "_ideal_index_distribution.npy") if not self.use_ideal_distribution: self.use_ideal_distribution = np.read( fName + "_ideal_index_distribution.npy")
def read_arr(fpath): if fpath.endswith('.npy'): return np.read(fpath) elif fpath.endswith('.h5'): return read_h5arr(fpath) else: raise KeyError(fpath)
#============================================================================== # Read in and convert the file #============================================================================== if args.filename.endswith("csv"): data = np.loadtxt(args.filename, delimiter=",", skiprows=1, dtype=str) sani_data = np.zeros((data.shape[0], 3)) for i, (day, time, temp, humid) in enumerate(data): date_str = "{} {}".format(day, time) date = datetime.datetime.strptime(date_str, "%m/%d/%y %H:%M") sani_data[i, 0] = date.timestamp() sani_data[i, 1] = float(temp) sani_data[i, 2] = float(humid) elif args.filename.endswith("npy"): sani_data = np.read(args.filename) else: sys.exit("We can't read that file format yet") # Zero the time column if we want to if args.relative: sani_data[:, 0] -= sani_data[0, 0] #np.save("temp-humi-data.npy", sani_data) #============================================================================== # Plotting #============================================================================== title = "Physical Quantities Trend" fontsize = 16
import numpy as np import matplotlib.pyplot as plt text = np.read('words.txt') text = text[:5] np.savetxt('data.txt', text) data2 = np.loadtxt('data.txt') print(data2)
print("IndexError: list index out of range") finally: print("In here, we try to dereference the value at index out of bound of list\n") # Handling TypeError try: print(3 + '3') except: print("TypeError: unsupported operand type(s) for +: \'int\' and \'str\'") finally: print("In here, we try to add a number to string literal\n") # Handling AttributeError try: import numpy numpy.read(10) except: print("AttributeError: module \'numpy\' has no attribute \'read\'") finally: print("In here, we try to use an attribute named \'read\' with the numpy library which indeed doesn't exist\n") # Handling FileNotFoundError try: f = open("x.txt", "r") except: print("FileNotFoundError: [Errno 2] No such file or directory: \'x.txt\'") finally: print("In here, we try to open a file with no existence\n") # Handling ZeroDivisionError try:
import numpy as np features = np.read("numpy_features.npy")