Öffnen Sie die Datei mbox-short.txt und lesen Sie Zeile für Zeile. Wenn Sie eine Zeile finden, die mit ‚Aus‘ wie die folgenden Zeile beginnt:Python Fehler: TypeError: 'int' Objekt ist nicht aufrufbar
From [email protected] Sat Jan 5 09:14:16 2008
Sie die Zeile mit split() analysieren und das zweite Wort in der Zeile ausdrucken (dh die gesamte Adresse der Person wer hat die Nachricht gesendet). Dann drucken Sie eine Zählung am Ende aus.
Hinweis: Stellen Sie sicher, dass die Zeilen, die mit 'From:' beginnen, nicht eingeschlossen sind.
Link for mbox-short.txt Datei: http://www.pythonlearn.com/code/mbox-short.txt
fopen = raw_input('Enter the file name you want to open: ')
fname = open(fopen)
line = 0
count = 0
pieces = 0
email = list()
for line in fname:
lines = line.rstrip()
if not line.startswith('From '):
continue
pieces = line.split()
print pieces[1]
print 'There were' ,count(pieces[1]), 'lines in the file with From as the first word
ich es geschafft, die richtige Ausgabe zu erhalten, bis zum letzten Drucknachricht.
Ausführung:
Enter the file name you want to open: mbox-short.txt
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
Traceback (most recent call last):
print 'There were' ,count(pieces[1]), 'lines in the file with From as the first word'
TypeError: 'int' object is not callable
Ich bin nicht sicher, warum ich dieses Traceback bin immer.
An der Spitze Ihres Skripts haben Sie 'count = 0', die nicht aufrufbar ist, d. H., Funktion/Klasse/etc. Was hast du davon erwartet? – Reti43
'count' ist eine Variable, keine Funktion. Ich denke, wenn Sie nur verwenden: 'print 'Es gab', Stücke [1], 'Zeilen in der Datei mit From als erstes Wort'' sollte es funktionieren ... – thefoxrocks
Wie die anderen Antworten haben gesagt:' count' ist keine Funktion, also verstehe ich nicht, warum Sie erwarten, dass es funktioniert. –