Python代码:def konto_all(): lst = [[], []] for index, x in enumerate(bankstm): kontoall = x["FIELD7"].replace(",", "."), x["FIELD8"].replace(",", ".") if kontoall != "" and index > 1: lst[0].append(kontoall) lst[1].append(x["FIELD1"].replace("-", "")) return(lst) kontoall1 = konto_all()kontoall = kontoall1[0]print(kontoall)杰森文件: { "FIELD1": "2020-05-27", "FIELD7": "", "FIELD8": "56.00" }, { "FIELD1": "2020-05-25", "FIELD7": "109.00", "FIELD8": "" }, { "FIELD1": "2020-05-20", "FIELD7": "78.90", "FIELD8": "" }, { "FIELD1": "2020-05-19", "FIELD7": "56.90", "FIELD8": "" }, { "FIELD1": "2020-05-19", "FIELD7": "46.80", "FIELD8": "" }, { "FIELD1": "2020-05-18", "FIELD7": "", "FIELD8": "400.00" },电流输出:[('', '56.00'), ('109.00', ''), ('78.90', ''), ('56.90', ''), ('46.80', ''), ('', '400.00')]想要的输出:[56.00, -109, -78.90, -56.90, -46.80, 400.00]FIELD7 中的每个字符串都应该是负浮点数。FIELD8 中的每个字符串都应该是正浮点数。
1 回答
Helenr
TA贡献1780条经验 获得超3个赞
这行:
kontoall = x["FIELD7"].replace(",", ".") , x["FIELD8"].replace(",", ".")
实际上是创建一个元组,这会导致[(val, ""), ("", val2), ...]
.
将该行更改为以下内容,它将按预期工作:
kontoall = float(x["FIELD7"].replace(",", ".")) or -float(x["FIELD8"].replace(",", "."))
kontoall
这里,如果 field7 的值是 field8 的负数,则我们将 value 设置为 field7 的值,并将其转换为浮点数。
添加回答
举报
0/150
提交
取消