gpt4 book ai didi

Python:以 10 为底的 int() 的无效文字: '808.666666666667'

转载 作者:太空狗 更新时间:2023-10-29 17:24:30 24 4
gpt4 key购买 nike

我有一个脚本可以解析来自 csv 文件的数据。下面一行给我带来了问题:

countData.append([timeStamp,int(my_csv[row][5])])

它给了我以下错误:

ValueError: invalid literal for int() with base 10: '808.666666666667' 

csv文件的行如下:

2013-06-12 15:09:00,svcName,0,0,10,808.666666666667

我试过在 python 提示符下运行 int(808.666666666667) 并且运行正常。

最佳答案

来自 int 的帮助:

int(x, base=10) -> int or long

If x is not a number or if base is given, then x must be a string or Unicode object representing an integer literal in the given base.

因此,对于任何 base'808.666666666667'int 的无效文字,使用:

>>> int(float('808.666666666667' ))
808

int(808.666666666667) 运行良好,因为您传递的是 float ,而不是字符串文字。

关于Python:以 10 为底的 int() 的无效文字: '808.666666666667',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17557870/

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