私は現在、友人によって書かれたいくつかのコードの変更である、最初の Python の取り組みを行っています。私はpython 2.6.6を使用しています。動作する元のコードは、私の非営利団体へのクレジット カードによる寄付のデータのログ ファイルから情報を抽出します。私の新しいバージョンは、いつの日か動作するようになれば、paypal による寄付に対して同じタスクを実行します。ログ ファイルは似ていますが、フィールド名が異なり、その他の違いがあります。
私が得ているエラーメッセージは次のとおりです。
トレースバック (最新の呼び出しが最後): ファイル "../logparse-paypal-1.py"、196 行目、convert_log(sys.argv[1], sys.argv[2], access_ids) ファイル "../logparse -paypal-1.py"、170 行目、convert_log の出力 = [output_fns の f の f(record, access_ids)] TypeError: 'str' object is not callable
このエラー メッセージに関連するこのフォーラムの投稿をいくつか読みましたが、今のところまだ航海中です。問題の可能性が高いオブジェクト (access_ids) に関連するコードの部分と、最初に使用したコードとの間に結果的な違いが見つかりません。access_ids テーブルに関連して私が行ったのは、スクリプトがテーブルで見つけた問題を出力し、一部のデータを無視する原因となったいくつかの行を削除することだけでした。それをしている間にキャラクターか何かを変更したのかもしれませんが、今のところ何も見つけられません。
これらのエラー メッセージを生成しているコードの部分は次のとおりです。
# Use the output functions configured above to convert the
# transaction record into a list of outputs to be emitted to
# the CSV output file.
print "Converting %s at %s to CSV" % (record["type"], record["time"])
output = [f(record, access_ids) for f in output_fns]
j = 0
while j < len(output):
os.write(csv_fd, output[j])
if j < len(output) - 1:
os.write(csv_fd, ",")
else:
os.write(csv_fd, "\n")
j += 1
convert_count += 1
print "Converted %d approved transactions to CSV format, skipped %d non-approved transactions" % (convert_count, skip_count)
if __name__ == '__main__':
if len(sys.argv) < 3:
print "Usage: logparse.py INPUT_FILE OUTPUT_FILE [ACCESS_IDS_FILE]"
print
print " INPUT_FILE Silent post log containing transaction records (must exist)"
print " OUTPUT_FILE Filename for the CSV file to be created (must not exist, will be created)"
print " ACCESS_IDS_FILE List of Access IDs and email addresses (optional, must exist if specified)"
sys.exit(-1)
access_ids = {}
if len(sys.argv) > 3:
access_ids = load_access_ids(sys.argv[3])
convert_log(sys.argv[1], sys.argv[2], access_ids)
行 170 は次のとおりです。 output = [f(record, access_ids) for f in output_fns]
196行目はこれです: convert_log(sys.argv[1], sys.argv[2], access_ids)
おそらく問題に関連する access_ids 定義は次のとおりです。
def access_id_fn(record, access_ids):
if "payer_email" in record and len(record["payer_email"]) > 0:
if record["payer_email"] in access_ids:
return '"' + access_ids[record["payer_email"]] + '"'
else:
return ""
else:
return ""
と
def load_access_ids(filename):
print "Loading Access IDs from %s..." % filename
access_ids = {}
for line in open(filename, "r"):
line = line.rstrip()
access_id, email = [s.strip() for s in line.split(None, 1)]
if not email_address.match(email):
continue
if email in access_ids:
access_ids[string.strip(email)] = string.strip(access_id)
return access_ids
これに関するアドバイスをよろしくお願いします。
- デイブ