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

标签 python

我有(匿名化)一个由 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/

相关文章:

python - SQLite Python AND 语句不起作用

python - 使用 scipy.stats.kstest 进行两样本 Kolmogorov-Smirnov 检验

python - 如何从 tf.dataset 打印样本记录?

python - 如何 reshape 数据,以便将具有多个观察值的 ID 按 ID 分组为所有可能的观察对?

python - 将 python 列表存储到数据库的最佳方法?

python - 如何在 linux 上通过 python + stem 获取 Tor 中继信息?

python - 从列表内的字符串中删除所有非数字字符

python - 使用 `nbytes` 广播后,numpy 数组中的 `broadcast_to` 值错误

python - 如何查找并单击包含多个类的按钮元素

python 信息函数 : where is it?