# hwbfiles ver.2 (11-9, 11-5-2022, S.Hayashi) import numpy as np #class fread: class fileRead: # file format: header = 'x', 'i-y', 'z' # if first letter is i or I, the item is an integer # data 1 = '2.5', 10, '0.1' # data 2 = '3.5', 12, '0.2' # ... # number of data equals number of data lines # number of items is limited by maxitem, i.e. 3 def __init__(self,fname,check): xa = [] ya = [] za = [] maxitem = 3 isf = np.arange(maxitem) # dx = np.arange(maxitem) file = open(fname, 'r', encoding='utf-8') a_line = file.readline() self.header = a_line l = len(a_line) a_line = a_line[:l-1] result = a_line.split(',') if check: print('Header = ',self.header) for x in reversed(result): # remove "" if len(x)>0: break else: result.remove(x) n_item = len(result) for i in range(n_item): s = result[i].upper() isf[i] = (s[0] != 'I') if isf[i]==True: print('float, ', end="") else: print('integer, ', end="") print() i = 0 while (True): a_line = file.readline() l = len(a_line) a_line = a_line[:l-1] if len(a_line)==0: # end of file break result = a_line.split(',') if check: print(i+1,') ',result) if isf[0]: xa.append(float(result[0])) else: xa.append(int(result[0])) if n_item > 1: if isf[1]: ya.append(float(result[1])) else: ya.append(int(result[1])) if n_item > 2: if isf[2]: za.append(float(result[2])) else: za.append(int(result[2])) if check: print(i+1,') ',result[0],' ', end='') if n_item>1: print(result[1],' ', end='') if n_item>2: print(result[2]) print() i += 1 file.close() self.fname = fname self.ndata = i self.nitem = n_item self.xa = xa self.ya = ya self.za = za #def fileRead(f,c): # return fread(f,c)