/ Published in: Python
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#!/bin/env python #------------------------------------------------------------------------------- import csv file = '/tmp/225056.txt' class parseDynamicCsv(object): ''' This class reads take a CSV file as an argument. The CSV file is read in to a list object. The last line of the list is popd off to determine the number of field values in the CSV. several methods are created for accessing the following data: * The colum count returned as a string. * Only the header lines as determined by the col count. * Only the data lines (minus last line) of the CSV. known bug: last line of each CSV file is not added back to the list. ''' def __init__(self, file): self.book1 = csv.reader(open(file)) self.csv = list() for line in self.book1: self.csv.append(line) self.csvLastline = self.csv[-1] for row in self.csvLastline: self.csvNumOfCols = len(row.split()) def csvColCount(self): return '%d' % self.csvNumOfCols def csvData(self): self.csvDataOnly = self.csv[self.csvNumOfCols:] return self.csvDataOnly def csvHeading(self): self.csvHeadingOnly = self.csv[:self.csvNumOfCols] return self.csvHeadingOnly def __strp__(self): for line in self.csv: self.lines += '%s\n' % (line) return self.lines def __repr__(self): self.lines = str() for line in self.csv: self.lines += '%s\n' % (line) return self.lines