ファイルmbox-short.txtを開き、1行ずつ読み込みます。あなたは次の行LIKE「から」で始まる行を見つけた場合:Pythonエラー:TypeError: 'int'オブジェクトが呼び出し可能ではありません
From [email protected] Sat Jan 5 09:14:16 2008
あなたはスプリットを()を使用して、ラインから解析し、(ラインで人のすなわち全体のアドレスを2番目の単語を出力します誰がメッセージを送ったか)。そして、最後にカウントを表示します。
ヒント: 'From:'で始まる行は含めないでください。 MBOX-short.txtファイルの
リンク: 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
は私が最後の印刷メッセージまで、正しい出力を得ることができました。
実行:
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
私は、このトレースバックを取得しています理由はわかりません。
スクリプトの先頭には、 'count = 0'があります。これは呼び出し可能ではなく、function/class/etcです。あなたはそれが何をすると思いましたか? – Reti43
'count'は変数であり、関数ではありません。私はあなたがちょうど使用すると思います: 'print '最初の単語から'ファイル'の中に 'pieces [1]、' linesがあります.'それはうまくいくはずです... – thefoxrocks
他の答えは 'count'は関数ではないので、なぜそれが動作すると思うのか分かりません。 –