以下Python代码未按预期运行。
最终输出缺少我期望在output_data[1] == "S"
输出的所有行。
我的懒惰尝试混淆客户端ID也失败了。
import csv
TID = 0
ACCT_TYPE = 1
PRODUCT_TYPE = 2
NUM_ACCT_OWNERS = 3
NUM_ACCT = 4
COMBINED_BALANCES = 5
TID_BALANCES = 6
INSURED_BALANCES = 7
ESTIMATED_UNINSURED = 8
FOREIGN_IND = 9
#output_data = [None] * 10
header_row_processed = False
with open("U:\exampletext.txt", 'r') as csvfile:
CIN = csv.reader(csvfile, delimiter="\t")
file_output_data = []
for row in CIN:
if not header_row_processed:
header_row_processed = True
continue
if not row[ACCT_TYPE] == "I" or row[ACCT_TYPE] == "S":
continue
if not row[PRODUCT_TYPE] == "Total":
continue
output_data = [None] * 6
client_id = row[TID]
client_id.replace("0", "x")
client_id.replace("1", "d")
client_id.replace("2", "h")
client_id.replace("3", "g")
client_id.replace("4", "v")
client_id.replace("5", "n")
client_id.replace("6", "m")
client_id.replace("7", "q")
client_id.replace("8", "w")
client_id.replace("9", "r")
client_id.replace("-", "u")
output_data[0] = client_id
output_data[1] = row[ACCT_TYPE]
output_data[2] = row[PRODUCT_TYPE]
output_data[3] = row[COMBINED_BALANCES]
output_data[4] = str(min(float(row[COMBINED_BALANCES])/float(row[NUM_ACCT_OWNERS]), 250000*float(row[NUM_ACCT_OWNERS])))
output_data[5] = str(max(0, (float(row[COMBINED_BALANCES])/float(row[NUM_ACCT_OWNERS]))-(250000*float(row[NUM_ACCT_OWNERS])))*(float(row[NUM_ACCT_OWNERS])))
file_output_data.append("\t".join(output_data))
#file_output_data = ','.join(str(v) for v in value_list)
if output_data[1] == "I":
out_file = open(
'U:\path_one.txt', 'w')
out_file.write("\n".join(file_output_data))
out_file.close()
if output_data[1] == "S":
out_file = open(
'U:\path_two.txt', 'w')
out_file.write("\n".join(file_output_data))
out_file.close()
答案 0 :(得分:0)
您的错误出在以下部分:
client_id = row[TID]
client_id.replace("0", "x")
client_id.replace("1", "d")
client_id.replace("2", "h")
client_id.replace("3", "g")
client_id.replace("4", "v")
client_id.replace("5", "n")
client_id.replace("6", "m")
client_id.replace("7", "q")
client_id.replace("8", "w")
client_id.replace("9", "r")
client_id.replace("-", "u")
output_data[0] = client_id
为它编写单元测试并查看代码的作用。替换不会做你认为它做的事情。