Ich hab jetzt meine Version auch nochmal ein wenig überarbeitet und das alles mit funktionen etwas übersichtlicher gemacht
Code: Alles auswählen
#!/usr/bin/python
# -*- coding: utf-8 -*-
import optparse
import time
def main():
start=time.time()
options,args,parser=parsing()
text,char2count=fileopen(options,args,parser)
if options.lower:
text = text.lower()
numworte,numchar,numchartotal=count(text,char2count)
output(numworte,numchar,numchartotal,char2count,options)
if options.test:
test(start)
def parsing():
parser = optparse.OptionParser()
parser.set_usage("%prog [options] File")
parser._add_version_option()
parser.version="%prog 0.1"
parser.add_option("-c","--case-sensitive",action="store_false", dest="lower",default=True,help="don't convert Letters to lowercase")
parser.add_option("-p","--percentage",action="store_true", dest="percentage",default=False,help="Show Percentage of default Characters")
parser.add_option("-P","--Percentage",action="store", type="string", dest="charcount",help="Show Percentage of charactres spezified in File",metavar="charfile")
parser.add_option("-s","--special",action="store_true", dest="special",default=False,help="Analyse special charactres")
parser.add_option("-t","--test",action="store_true", dest="test",default=False,help="Make a Speedtest")
(options, args) = parser.parse_args()
return options,args,parser
def fileopen(options,args,parser):
if len(args)>0:
try:
daten = file (args[0],'r')
text = daten.read()
daten.close()
except IOError, err:
print 'Error when reading File "%s":\n\t%s' % (err.filename, err)
parser.exit()
else:
parser.print_help()
parser.exit()
if options.charcount is not None:
try:
chardaten = file (options.charcount,'r')
chartext = chardaten.read()
chardaten.close()
char2count=list(chartext)
except IOError, err:
print 'Error when reading File "%s":\n\t%s' % (err.filename, err)
parser.exit()
else:
char2count=set(options)
return text,char2count
def set(options):
if options.lower:
char2count=list("abcdefghijklmnopqrstuvwxyz")
else:
char2count=list("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz")
if options.special:
char2count.append("--")
char2count.extend(" ")
spezial_char=".,;.´:-_#'+*~!?§$€%&()[]{}=\/|<>"
char2count.extend(spezial_char)
return char2count
def count (text,char2count):
numworte=len(text.split())
numchar = [text.count(char) for char in char2count]
numchar = map(text.count, char2count)
numchartotal = sum(numchar)
return numworte,numchar,numchartotal
def output(numworte,numchar,numchartotal,char2count,options):
percentage = options.percentage
print "Anzahl der Worte:",numworte
print "Anzahl der Buchstaben:",numchartotal
print "Durchschnittliche Anzahl der Buchstaben pro Wort:",numchartotal/numworte
if percentage:
print "Anteile der Zeichen"
for i in range(len(char2count)):
char_tmp=char2count[i]
if char_tmp != "--":
num_tmp=float(numchar[i])
numchartotal=float(numchartotal)
proz=(num_tmp/numchartotal)*100
print "Der Anteil von",char_tmp,"ist","%5.2f" % proz,"% Absolut:",numchar[i]
else:
print "------------------------------------------------------"
def test(start):
print "Runtime: %.4f"%(time.time()-start),"Sekunden"
print "Cputime: %.4f"%(time.clock()),"Sekunden"
if __name__ == '__main__':
main()