def load_info(f_name): """ loads the header, header_length and number of data columns in a file arguments: f_name: <*.csv> (string) the name of the file return values: n_cols: (int) the number of columns h_len: (int) the number of rows in the header header: (((string) list) list) a list of the list of the strings in each "cell" of the header """ f_stream = open(f_name, "r") h_len = 0 n_cols = 0 header = [] while True: line = f_stream.readline() if line == "" or line == "\n": break segs = line.split(',') try: csvd.string_to_datetime(segs[0]) break except (ValueError, AttributeError): header.append(segs) h_len += 1 n_cols = len(header[-1]) f_stream.close() return n_cols, h_len, header
def load_csv_file_opti(self): """ a faster load file """ for idx in range(self.m_numcols): self.m_datacols.append([]) f_stream = open(self.m_name, "rb") f_stream.seek(-2,2) while f_stream.read(1) != "\n": f_stream.seek(-2,1) line = f_stream.readline() if line.strip() == "": return if line[:line.find(',')] == self.m_header[-1][0]: return cells = line.split(",") for col in range(len(cells)): if col == 0: self.m_datacols[col].append(csvd.string_to_datetime(cells[col])) else: self.m_datacols[col].append(float(cells[col])) f_stream.close() #store last date in the file try: self.m_last_init_date = self.m_datacols[0][-1] except IndexError: pass
def load_file_to_plot(f_name, skip = 4): """ loads a array of a datetime.datetime object and an array of coorisponding values arguments f_name: (string)the file name skip: (int) number of rows to skip (defaluts to 4 the data logger header length) """ date_string , value = numpy.loadtxt(f_name, dtype = {'names':('date' , 'val'),'formats':('S100','f4')}, delimiter=',', usecols=(0 , 1), skiprows=skip, unpack=True) date = [] for items in date_string: date.append(csvd.string_to_datetime(items)) date = numpy.array(date) return date, value
def load_csv_file(self): """ loads a csv file replaces the csv_utitlies version """ f_stream = open(self.m_name, "r") f_text = f_stream.read() f_stream.close() rows = f_text.replace("\r","").split("\n") for idx in range(self.m_numcols): self.m_datacols.append([]) if len(rows) == self.m_headlen: return for item in rows[self.m_headlen:]: if item == "": continue cells = item.split(",") for col in range(len(cells)): if col == 0: self.m_datacols[col].\ append(csvd.string_to_datetime(cells[col])) else: try: self.m_datacols[col].append(float(cells[col])) except ValueError: self.m_datacols[col].append(str(cells[col])) #store last date in the file try: self.m_last_init_date = self.m_datacols[0][-1] except IndexError: pass