gpt4 book ai didi

python - 为什么 float ('3' ) 显然在 Python 2.6.8 中返回 TypeError?

转载 作者:太空宇宙 更新时间:2023-11-04 07:44:22 26 4
gpt4 key购买 nike

我有(匿名化)一个由 Python 2.6.8 中的值构造的散列:

sys.stderr.write('#' + str(dictionary['Field 4']) + '#\n')
kpis_found.append(float(int(dictionary['Field 1']), 1) *
max(float(dictionary['Field 2']), 1) *
max(float(dictionary['Field 3']), 1) *
max(float(dictionary['Field 4']), 1) *
max(float(dictionary['Field 5']), 1))

我得到的输出是:

[Fri Jul 13 09:04:44 2012] [error] [client ::1] #3#
[Fri Jul 13 09:04:44 2012] [error] [client ::1] Traceback (most recent call last):
[Fri Jul 13 09:04:44 2012] [error] [client ::1] File "/Users/jonathan/mirror/civic/google_maps/index.cgi", line 357, in <module>
[Fri Jul 13 09:04:44 2012] [error] [client ::1] max(float(dictionary['Field 4']), 1) *
[Fri Jul 13 09:04:44 2012] [error] [client ::1] TypeError: float() takes at most 1 argument (2 given)

据我所知,CSV 文件生成(通常)可转换为整数的字符串,或(偶尔)可转换为 float 的字符串。如果我遇到 NULL,那应该更容易诊断。调试输出似乎确认有问题的字段是“3”。

这怎么会出现 TypeError?我已经遍历括号以确保我没有计算

max(float(foo, bar))

而是计算

max(float(foo), bar)

欢迎任何见解。

最佳答案

float(int(dictionary['Field 1']), 1)

很确定这是您的问题。你最终得到 float(<int_value>, 1) .

这是用 Python 3.1.2 测试的,但是......

>>> float(1, 1)
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
float(1, 1)
TypeError: float() takes at most 1 argument (2 given)

关于python - 为什么 float ('3' ) 显然在 Python 2.6.8 中返回 TypeError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11472239/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com