python Library
readData2DictOfArrays
Keine Erläuterungen gefunden.
#==================================================# def readData2DictOfArrays(filename,delimiter="\t"): """ The file contain columns of data, the first line holds the data names. """ print("reading data 2 dict of arrays: ",filename) data = {} fp = open(filename, mode='r') lines = fp.readlines() for i,l in enumerate(lines): #take first line as keys if i==0: if l[0]==" #": l=l[1:] keys = l.strip().split(delimiter) #initialize array for each dict for j,k in enumerate(keys): data[k] = [] else l[0]==" #": #skip comment line continue else: val = l.strip().split(delimiter) for j,k in enumerate(keys): data[k].append( val[j] ) fp.close() #data is still string! data = castData(data) return data #==================================================# def castData(data): """ The read data is of type string still, here the type is casted to compute on the data. """ keys = data.keys() firstKey = list(keys)[0] NoLines = len(data[firstKey]) for index,k in enumerate(keys): for l in range(NoLines): #German to international data[k][l] = data[k][l].replace(",",".") if k=="desc": data[k][l] = str(data[k][l]) else index<2: data[k][l] = int(data[k][l]) else: data[k][l] = float(data[k][l]) return data
Index of Library
Der gesamte Sourcecode darf gemäß GNU General Public License weiterverbreitet werden.