Advertisement
Guest User

Untitled

a guest
Nov 20th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.81 KB | None | 0 0
  1. def main():
  2.     fh = openFile('NFL2011QB.csv')
  3.     fd = readFile(fh)
  4.     fh.close
  5.  
  6.     floatFields = []
  7.     intFields = []
  8.  
  9.     headers = getHeaders(fd)
  10.     parseData(fd, headers)
  11.     typeCastFloatFields(fd, floatFields)
  12.     typeCastIntFields(fd, intFields)
  13.     print(fd)
  14.  
  15. def tdReport(L):
  16.     L.sort(key=tdKey)
  17.     L.reverse()
  18.     for item in L:
  19.         print(item['TD '])
  20.  
  21. def tdKey(line):
  22.     return line['TD ']
  23.  
  24. def typeCastFloatFields(L, fields):
  25.     '''
  26.    (list of dicts by field keys) -> list
  27.    Casts dictionary key values to floats if possible
  28.    '''
  29.     for ix in range(len(L)):
  30.         for field in fields:
  31.             L[ix][field] = float(L[ix][field])
  32.     return L
  33.  
  34. def typeCastIntFields(L, fields):
  35.     '''
  36.    (list of dicts by field keys) -> list
  37.    Casts dictionary key values to integers if possible
  38.    '''
  39.     for ix in range(len(L)):
  40.         for field in fields:
  41.             L[ix][field] = int(L[ix][field])
  42.     return L
  43.  
  44. def conditionFields(field):
  45.     if len(field) == 0 or '':
  46.         return 0
  47.     try:
  48.         return int(field)
  49.     except:
  50.         try:
  51.             return float(field)
  52.         except:
  53.             return field
  54.  
  55. def readFile(fh):
  56.     '''
  57.    (textIOWrapper / file handle) -> list of strings
  58.    Reads files into lists by lines with readlines(),
  59.    Strips off white space if any with strip()
  60.    Removes likes with no data with pop()
  61.    '''
  62.     L = fh.readlines()
  63.  
  64.     for ix in range(len(L)):
  65.         L[ix] = L[ix].strip()
  66.  
  67.     for ix in range(len(L)-1, -1, -1):
  68.         if len(L[ix]) == 0:
  69.             L.pop(ix)
  70.     return L
  71.  
  72. def openFile(fname):
  73.     '''
  74.    (str) ->textIOWrapper and file handler. None if error.
  75.    '''
  76.     return open(fname, 'r')
  77.  
  78. def getHeaders(L):
  79.     '''
  80.    (List) -> list of parsed header values.
  81.    Pops the first line, return as comas delimited headers
  82.    '''
  83.     hl = L.pop(0)
  84.     return hl.split(',')
  85.  
  86. def parseDataLines(s):
  87.     '''
  88.    (str of csv) -> list of values
  89.    Brings strings into fields and removes unwanted commas.
  90.    '''
  91.     L = s.split(',')
  92.     if len(L) > 21:
  93.         L[20] = L[20] + ',' + L[21]
  94.         L.pop(20)
  95.  
  96.     if len(L) > 21:
  97.         L[20] = L[20] + ',' + L[21]
  98.         L.pop(20)
  99.     L[20] = L[20][1:-1]
  100.     return L
  101.  
  102. def parseData(L, headers):
  103.     '''
  104.    (List of values, headers)
  105.    takes list data and breaks items down into lists
  106.    '''
  107.     for ix in range(len(L)):
  108.         L[ix] = parseDataLines(L[ix])
  109.         L[ix] = makeDicts(L[ix], headers)
  110.  
  111. def makeDicts(L, headers):
  112.     '''
  113.    (list, list of headers) -> dictionary of headers and data
  114.    Converts list to dictionary with header values as keyes
  115.    '''
  116.     D = {}
  117.     for ix in range(len(headers)):
  118.         D[headers[ix]] = L[ix]
  119.     return D
  120.  
  121. if __name__ == '__main__':
  122.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement