Return to Snippet

Revision: 43222
at March 19, 2011 15:38 by mattsn0w


Updated Code
#!/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

Revision: 43221
at March 19, 2011 10:07 by mattsn0w


Updated Code
#!/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.pop()
        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

Revision: 43220
at March 19, 2011 10:06 by mattsn0w


Updated Code
#!/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 pop'd 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.pop()
        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

Revision: 43219
at March 19, 2011 10:05 by mattsn0w


Initial Code
#!/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 pop'd 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.pop()
        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

Initial URL


Initial Description


Initial Title
Basic class to parse variable field CSV files.

Initial Tags
csv

Initial Language
Python