Advertisement
Guest User

Untitled

a guest
Jan 31st, 2014
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.75 KB | None | 0 0
  1. import csv,os,fnmatch
  2.  
  3. def main(folder,index,query):
  4.     filenames = gen_find('*.csv',folder)    
  5.     files = readcsv(filenames)
  6.     lines = files_cat(files)
  7.     data = makerow(lines)
  8.     spreadsheet = makedict(data,'Title')
  9.     print(standarddev(spreadsheet,query))
  10.  
  11.  
  12. def average(dictionary,key):
  13.     total,n=0,0
  14.     for element in dictionary:
  15.         try:
  16.             total += float(dictionary[element][key])
  17.             n+=1
  18.         except:
  19.             pass
  20.     return total/(n+1)
  21.  
  22. def standarddev(dictionary,key):
  23.     a = average(dictionary,key)
  24.     total,n=0,0
  25.     for element in dictionary:
  26.         try:
  27.             total += (a-float(dictionary[element][key]))**2
  28.             n+=1
  29.         except:
  30.             pass
  31.     return a,total/n,(total/n)**0.5
  32.            
  33. def makedict(generator,index):
  34.     d = {}
  35.     for element in generator:
  36.         d[element[index]]=element
  37.     return d
  38.    
  39.  
  40. def files_cat(generator):
  41.     for file in generator:
  42.         try:
  43.             for line in file:
  44.                 yield line
  45.         except:
  46.             pass
  47.  
  48. def makerow(lines):
  49.     header = next(lines)
  50.     for line in lines:
  51.         if line !=header:
  52.             yield dict(zip(header,line))
  53.          
  54.        
  55. def readcsv(filenames):
  56.     for file in filenames:
  57.         csvfile = open(file,newline='')
  58.         dialect = csv.Sniffer().sniff(csvfile.read(1024))
  59.         csvfile.seek(0)
  60.         reader = csv.reader(csvfile,dialect)
  61.         yield reader
  62.        
  63. def gen_find(filepat,top):
  64.     for path, dirlist, filelist in os.walk(top):
  65.         for name in fnmatch.filter(filelist,filepat):
  66.             yield os.path.join(path,name)
  67.  
  68. def trace(source):
  69.     for item in source:
  70.         pp.pprint(item)
  71.         yield item
  72.  
  73. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement