CS Student hier ... Vielleicht liegt es an mangelndem Schlaf, aber ich kann nicht herausfinden, warum meine Zählimpulse überhaupt nicht auftauchen. Die Formatierung ist korrekt, aber ich kann nicht herausfinden, warum keine Zählung angezeigt wird. Jede Hilfe wird geschätzt.Die Anzahl der Wörterbücher wird nicht angezeigt
Beschreibung:
liest ein Kundeninformationen kleiner Unternehmen aus dem Text Datei: customerData.txt in eine Kundenliste. Jeder Kundendatensatz ist unter eine einzelne Zeile in der Textdatei und erzeugt einen Eintrag in der Kundenliste , die zufällig eine Liste von 12 Zeichenkettenfeldern ist. Die Reihenfolge der Felder lautet Vorname, Vorname, Nachname, Straße, Stadt, Bundesland, Postleitzahl, Land, E-Mail-Adresse, Telefonnummer, Geschlecht und Geburtstag.
Die customerList wird verwendet, um ein Wörterbuch mit einer Anzahl von Kunden aus jedem Staat zu generieren.
Dieses Wörterbuch wird in zwei Reihenfolgen gedruckt: nach Statuscode sortiert und nach Statusanzahl sortiert.
Aktuelle Code:
def main():
""" Open's file, reads customer information into a list, closes the file"""
custFile = open('customerData.txt','r')
customerList = generateList(custFile)
custFile.close()
statesCountDictionary = generateStatesCountDictionary(customerList)
printSortedByState(statesCountDictionary)
printSortedByCount(statesCountDictionary)
def generateList(custFile):
""" Reads customer data from file and returns a list of customers"""
customers = []
for line in custFile:
custInfoList = line.strip().split(',')
customers.append(custInfoList)
return customers
def generateStatesCountDictionary(customerList):
""" Tallies the number of customers from each state and
returns a dictionary with the state as a key and the
count as the associated value."""
statesTallyDict = {}
for customer in customerList:
state = customer[5]
if state in statesTallyDict:
count = statesTallyDict.get(state) +1
else:
count = 1
return statesTallyDict
def printSortedByState(statesCountDictionary):
""" Prints the tally of the number of customers from each state
sorted alphabetically by the state abbreviation."""
tupleList = statesCountDictionary.items()
tupleList.sort()
print "\n\n%5s %-14s" % ("State","Customer Count")
print "-"*25
for item in tupleList:
state, count = item
print "%5s %8d" % (state.center(5), count)
def printSortedByCount(statesCountDictionary):
""" Prints the tally of the number of customers from each state
sorted from most to least."""
stateCountTupleList = statesCountDictionary.items()
countStateTupleList = []
stateCountTupleList.sort()
print "\n\n%5s %-14s" % ("Customer Count","State")
print "-"*25
for item in stateCountTupleList:
count, state = item
print "%8d %5s" %(count.center,state(5))
main()
Können Sie einige Beispielein-/ausgabe liefern? – TemporalWolf
Wenn Sie sagen: "Ich kann nicht herausfinden, warum keine Zählung angezeigt wird", meinen Sie, dass ein Fehler aufgetreten ist oder absolut nichts "gedruckt" oder "etwas unerwartetes drucken" ist? Es sieht auch so aus, als würden Sie eine CSV-Datei analysieren. –