简体   繁体   English

Python:ValueError:无法将字符串转换为浮点型:'4623634 0'

[英]Python: ValueError: could not convert string to float: '4623634 0'

Here is my code: 这是我的代码:

import csv
with open ("Filename1.txt") as f:
   dict1 = {}
   r = csv.reader(f,delimiter="\t")
   for row in r:

       a, b, v = row
       dict1.setdefault((a,b),[]).append(v)

   #for key in dict1:
      #print(key[0])
      #print(key[1])
      #print(d[key][0]])

with open ("Filename2.txt") as f:
   dict2 = {}
   r = csv.reader(f,delimiter="\t")
   for row in r:

       a, b, v = row
       dict2.setdefault((a,b),[]).append(v)

   #for key in dict2:
       #print(key[0])

    count = 0
    for key1 in dict1:
       for key2 in dict2:
          if (key1[0] == key2[0]) and abs(float(key1[1])) - (float(key2[1])) < 10000:
           count += 1   

Previously I was getting this error: 以前我遇到此错误:

Traceback (most recent call last):
File "/Users/macbookpro/Desktop/MainDict.py", line 28, in <module>
if key1[0] == key2[0] and abs(key1[1] - key2[1]) < 10000:
TypeError: unsupported operand type(s) for -: 'str' and 'str' 

So of course I tried turning those strings into integers. 因此,我当然尝试将这些字符串转换为整数。 However I then got this error: 但是我然后收到此错误:

 Traceback (most recent call last):
 File "/Users/macbookpro/Desktop/MainDict.py", line 28, in <module>
 if (key1[0] == key2[0]) and abs((int(key1[1])) - (int(key2[1]))) < 10000:
 ValueError: invalid literal for int() with base 10: '1002569 1'

Then I tried using float and now I get this error, which is where I am stuck now: 然后我尝试使用float,现在出现此错误,这就是我现在遇到的问题:

 Traceback (most recent call last):
 File "/Users/macbookpro/Desktop/MainDict.py", line 28, in <module>
 if (key1[0] == key2[0]) and abs(float(key1[1])) - (float(key2[1])) < 10000:
 ValueError: could not convert string to float: '2486997 2'

Here are examples of what my input files consist of: 以下是我的输入文件组成的示例:

Filename1 文件名1

1   11383002    8 1.16E-05
1   159962368   1.17E-05
2   133623587   1.26E-05
2   1002569 1   3.30E-06
3   168940139   1.40E-05
3   49736942    1.43E-05

Filename2 文件名2

10  11383002    8 1.16E-05
5   159962368   1.17E-05
7   133623587   1.26E-05
9   1002569 1   3.30E-06
8   168940139   1.40E-05
1   49736942    1.43E-05

Now my question is, why am I getting this error. 现在我的问题是,为什么会出现此错误。 Is there something in the code specifically? 代码中是否有特定内容? Or is there something wrong with my text files. 或者我的文本文件有问题。 What suggestions do you having on fixing this problem and how can I alter my code (if that's the case) to do so? 您对解决此问题有什么建议?如何更改我的代码(如果是这种情况)?

I can only guess what number 1002569 1 should be, but maybe use split(" ")[0] to get just the first part of it and convert that into an int ? 我只能猜测应该是什么数字1002569 1 ,但是也许可以使用split(" ")[0]来获取它的第一部分并将其转换为int

if ... and abs(float(key1[1].split(" ")[0])) - (float(key2[1].split(" ")[0])) < 10000:

This will also work if there is no space (like in the other rows). 如果没有空间(与其他行一样),这也将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM