我目前正在做我的第一个 python 工作,对朋友编写的一些代码进行修改。我正在使用 python 2.6.6。有效的原始代码从信用卡捐赠给我的非营利组织的数据日志文件中提取信息。如果有一天它可以工作,我的新版本将对贝宝进行的捐赠执行相同的任务。日志文件类似,但具有不同的字段名称和其他差异。
我收到的错误消息是:
回溯(最后一次调用):文件“../logparse-paypal-1.py”,第 196 行,在 convert_log(sys.argv[1], sys.argv[2], access_ids) 文件“../logparse -paypal-1.py",第 170 行,在 convert_log 输出 = [f(record, access_ids) for f in output_fns] 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
提前感谢您对此的任何建议。
- 戴夫