float()的无效文字:0.000001,如何修复错误? [英] Invalid literal for float(): 0.000001, how to fix error?

查看:341
本文介绍了float()的无效文字:0.000001,如何修复错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含3列数据的.csv文件.我需要创建一个新的输出文件,其中包括原始文件第一和第三列中的一组特定数据.第三列包含十进制值,我相信在这种情况下,我使用了python的float()功能.我尝试了以下代码:

I have a .csv file containing 3 columns of data. I need to create a new output file that includes a specific set of data from the first and third column from the original file. The third column contains decimal values, and I believe in such a case I have use the float() feature of python. I have tried the following code:

in_file = open("filename.csv", "r")

out_file = open("output.csv", "w")

while True:

    line = in_file.readline()
    if (line == ''): 
        break
    line = line.strip() 
    items = line.split(',') 
    gi_name = items[0] 
    if (gi_name.startswith("_"))
        continue
    p_value = float(items[2]) 
    if (p_value > 0.05):
        continue
    out_file.write(','.join([gene_name, str(p_value)]))
in_file.close()
out_file.close()

运行上述命令时,出现以下错误:

when I run the above, I recieve the following error:

错误:float()的字面值无效:0.000001

Error: invalid literal for float(): 0.000001

值0.0000001是我的数据集中第三列的第一个值,我猜代码无法读取超出该范围的值,但我不确定为什么.我是python的新手,并不真正理解为什么我收到此错误或如何修复它.我已经尝试过其他修改方式来输入float(),但是没有成功.有谁知道我怎么能解决这个问题?

the value 0.0000001 is the first value in my data set for the third column, and I guess the code cannot read beyond that set but I'm not sure why. I am new to python, and don't really understand why I am getting this error or how to fix it. I have tried other modifications for how to input the float(), but without success. Does anyone know how I might be able to fix this?

推荐答案

从您发布的内容来看,您不清楚要传递给float()的字符串是否存在某些细微的错误(因为它看起来完全合理).尝试添加调试打印语句:

From what you've posted, it's not clear whether there is something subtly wrong with the string you're trying to pass to float() (because it looks perfectly reasonable). Try adding a debug print statement:

print(repr(items[2]))
p_value = float(items[2])

然后,您可以准确确定 传递给float()的内容.调用repr()将使通常不可见的字符也可见.将结果添加到您的问题中,我们将能够进行进一步评论.

Then you can determine exactly what is being passed to float(). The call to repr() will make even normally invisible characters visible. Add the result to your question and we will be able to comment further.

这篇关于float()的无效文字:0.000001,如何修复错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆