In [28]:
from csv_util import Csv
print Csv.__doc__


Utility class for reading/writing CSV files.

    Args:
        - *fn*: CSV filename
        - *delimiter*: character (default ,); set to " " for whitespace-delimited files.
        - *comment*: ignore lines beginning with this (default #)
        - *numeric*: attempt to convert values to floats

    Methods:
        - *reload*: reload file
        - *keys*: get column titles
        - *values*: get column values as lists of strings
        - *items*: zip(keys(), values())
        - *float*: get column values as ndarray of floats, if possible
    
    You can access column values as lists using the ``[key]`` syntax on the
    class object, which behaves like a dictionary. The keys() method gives 
    a list of column headers.

    Attributes:
        - *delimiter*
        - *fn*
        - *txt*: text of CSV file (actually this is used rather than *fn* to
          read data from. You can definitely set this attribute directly if you like.)
        - *f*: file-like object built from *txt*, read-only
        - *DictReader*: :class:`csv.DictReader` object
        - *table*: list of list of string objects, each inner list is a row of the CSV
        - *ndarray*: convert table into an numpy ndarray (fails if there is
          non-numerical data in the CSV)

    

In [27]:
c = Csv("spdata.csv")

In [18]:
print ''.join(c.f.readlines()[:7])


date,time,easting,northing,res_kohm,rove-base,base-rove,notes,clean_notes,SP,repeat,distance_calc,line,difference,lag,distance_nominal,label_text,label_italic,label_bold,label_x,label_y,label_distance,label_rotation
22/11/2012,12:18:35,589350,6893861,nan,0,0,base; zero voltage by definition,Base electrode (bentonite),0,no,1309.85,S1,,,,,,,,,,
22/11/2012,12:19:35,589331,6893863,2.8,16,-17,bearing 264,,16.5,no,1291.08,S1,,,,1300,,,,,,
22/11/2012,12:22:40,589311,6893862,15.5,17,-17,,,17,no,1271.06,S1,,,, ,,,,,,
22/11/2012,12:24:00,589292,6893862,10,18,-19,,,18.5,no,1252.14,S1,,,, ,,,,,,
22/11/2012,12:25:20,589273,6893862,93,49,-49,very dry; gravels in creek bed,,49,no,1233.24,S1,,,, ,,,,,,
22/11/2012,12:26:55,589253,6893863,950,62,-61,very dry; gravels in creek bed,,61.5,no,1213.47,S1,,,,,,,,,,


In [23]:
print c.rows[3] # start counting from zero, excluding the header and any commented lines


['22/11/2012', '12:24:00', 589292.0, 6893862.0, 10.0, 18.0, -19.0, nan, nan, 18.5, 'no', 1252.14, 'S1', nan, nan, nan, ' ', nan, nan, nan, nan, nan, nan]

In [9]:
print c.keys()


['date', 'time', 'easting', 'northing', 'res_kohm', 'rove-base', 'base-rove', 'notes', 'clean_notes', 'SP', 'repeat', 'distance_calc', 'line', 'difference', 'lag', 'distance_nominal', 'label_text', 'label_italic', 'label_bold', 'label_x', 'label_y', 'label_distance', 'label_rotation']

In [25]:
print c["time"][:10]


['12:18:35', '12:19:35', '12:22:40', '12:24:00', '12:25:20', '12:26:55', '12:31:35', '12:33:55', '12:35:50', '12:38:20']

There are lots of other useful methods which I will document when I get some time!


In [ ]: