Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import csv,os,fnmatch
- def main(folder,index,query):
- filenames = gen_find('*.csv',folder)
- files = readcsv(filenames)
- lines = files_cat(files)
- data = makerow(lines)
- spreadsheet = makedict(data,'Title')
- print(standarddev(spreadsheet,query))
- def average(dictionary,key):
- total,n=0,0
- for element in dictionary:
- try:
- total += float(dictionary[element][key])
- n+=1
- except:
- pass
- return total/(n+1)
- def standarddev(dictionary,key):
- a = average(dictionary,key)
- total,n=0,0
- for element in dictionary:
- try:
- total += (a-float(dictionary[element][key]))**2
- n+=1
- except:
- pass
- return a,total/n,(total/n)**0.5
- def makedict(generator,index):
- d = {}
- for element in generator:
- d[element[index]]=element
- return d
- def files_cat(generator):
- for file in generator:
- try:
- for line in file:
- yield line
- except:
- pass
- def makerow(lines):
- header = next(lines)
- for line in lines:
- if line !=header:
- yield dict(zip(header,line))
- def readcsv(filenames):
- for file in filenames:
- csvfile = open(file,newline='')
- dialect = csv.Sniffer().sniff(csvfile.read(1024))
- csvfile.seek(0)
- reader = csv.reader(csvfile,dialect)
- yield reader
- def gen_find(filepat,top):
- for path, dirlist, filelist in os.walk(top):
- for name in fnmatch.filter(filelist,filepat):
- yield os.path.join(path,name)
- def trace(source):
- for item in source:
- pp.pprint(item)
- yield item
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement