python - Read multiple CSV Files, cut at empty row, sorted array of column -
i read multiple csv files python 3.6, skip header , stop @ first empty row. extract fourth column of each csv file fill array sorted shown @ end.
my files: csv files
thats code far i'm not happy output order , maybe there more elegant way?
csvdata_d=[] in range(timestep): # timestep = number of files open(csvname[i]) f: reader = csv.reader(f) readerlist=list(reader) row in readerlist[2:]: # skip 2 header lines if len(row) == 0: # empty line definition break else: row=list(row) csvdata_d.append(row[3]) # fourth column print(csvdata_d)
my output is: csvdata_d=['1.1', '1.2', '1.3', '2.1', '2.2', '2.3', '3.1', '3.2', '3.3']
but want [['1.1','2.1','3.1'],['1.2','2.2','3.2'],['1.3','2.3','3.3']]
thanks help.
i solved numpys genfromtxt module , loop find empty row
headsize=2 #lines toskip @ beginning csvdata=[] measuredata=[] open (csvname[0])as f: #measurement of file first empty row reader = csv.reader(f) readerlist=list(reader) lengthfile=(len(readerlist)) row in readerlist[headsize:]: if len(row)== 0: break else: measuredata.append(list(row[3])) skfooter=int(lengthfile-(len(measuredata)+(headsize+1))) #calc number of rows below empty line in range(timestep): data=np.genfromtxt((csvname[i]), delimiter=',', skip_header=headsize, usecols=3, skip_footer=skfooter) csvdata.append(data) s=[] in range (len(csvdata[0])): s.append([item[i] item in csvdata]) print(s)
Comments
Post a Comment