为什么我不能将str列表转换为浮点列表?

时间:2021-01-17 21:04:06

I'm starting to write a code, but it fails at the beginning.

我开始编写代码,但一开始就失败了。

This is my code:

这是我的代码:

import csv
reader = csv.reader(open("QstartRefseqhg19.head"), dialect='excel-tab' )

for row in reader:
    C = row[1].split(",")[1:]
    C1 = [float(i) for i in C]

    print C1 

and the error log says:

并且错误日志说:

Traceback (most recent call last):
  File "/home/geparada/workspace/SJtag/src/TagGen.py", line 8, in <module>
    C1 = [float(i) for i in C]
ValueError: empty string for float()

I've also tried

我也试过了

import csv
reader = csv.reader(open("QstartRefseqhg19.head"), dialect='excel-tab' )

for row in reader:
    C = row[1].split(",")[1:]       
    C1 = map(float, C)        

    print C1

My input file looks like this:

我的输入文件如下所示:

NM_032291   0,227,291,316,388,445,500,676,688,700,725,777,863,956,1031,1532,1660,1787,1847,1959,2115,2248,2451,2516,2681,   tttctctcagcatcttcttggtagcctgcctgtaggtgaagaagcaccagcagcatccatggcctgtcttttggcttaacacttatctcctttggctttgacagcggacggaatagacctcagcagcggcgtggtgaggacttagctgggacctggaatcgtatcctcctgtgttttttcagactccttggaaattaaggaatgcaattctgccaccatgatggaaggattgaaaaaacgtacaaggaaggcctttggaatacggaagaaagaaaaggacactgattctacaggttcaccagatagagatggaattcagcccagcccacacgaaccaccctacaatagcaaagcagagtgtgcgcgtgaaggaggaaaaaaagtttcgaagaaaagcaatggggcaccaaatggattttatgcggaaattgattgggaaagatataactcacctgagctggatgaagaaggctacagcatcagacccgaggaacccggctctaccaaaggaaagcacttttattcttcaagtgaatcggaagaagaagaagaatcacataagaaatttaatatcaagattaaaccattgcaatctaaagacattcttaagaatgctgcaactgtagatgaattgaaggcatcaataggcaacatcgcactttccccatcaccagtgaggaaaagtccgaggcgcagcccgggtgcaattaaaaggaacttatccagtgaagaagtggcaagacccaggcgttccacaccaactccagaacttataagcaaaaagcctccagatgacactacggcccttgctcctctctttggcccaccactagaatcagcttttgatgaacagaagacagaagttcttttagatcagcctgagatatggggttcaggccaaccaattaatccaagcatggagtcgccaaagttaacaaggccttttcccactggaacacctccaccactgcctccaaaaaatgtaccagctaccccaccccgaacaggatcccccttaacaattggaccaggaaatgaccagtcagccacagaggtcaaaattgaaaaactaccatccatcaatgacttggacagcatttttgggccagtattgtcccccaagtctgttgctgttaatgctgaagaaaagtgggtccatttttctgatacatccccggaacatgttactccggagttgactccaagggaaaaagtggtgtccccaccagctacaccagacaacccagctgactccccagctccaggccctctcggccccccaggtcccacaggccccccagggcctcctgggcctcctcgcaatgtactatcgccgctcaatttagaagaagtccagaagaaagtcgctgagcagaccttcattaaagatgattacttagaaacaatctcatctcctaaagattttgggttgggacaaagagcaactccacctcccccaccaccacccacctacaggactgtggtttcgtcccccggacctggctcgggccctggtccggggaccaccagtggtgcatcatcccctgctcgaccagccactcctttggttccttgcagaagtaccactccacctccacctcctccccggcctccatcccggccaaagctacctccaggaaaacctggagttggagatgtgtccagaccttttagccctcccattcattcttccagccctcctccaatagcacccttagcgcgggctgaaagcacttcttcaatatcgtcaaccaattccttgagcgcagccaccactcccacagttgagaatgaacagccttccctcgtttggtttgacagaggaaagttttatttgacttttgaaggttcttccaggggacccagccccctaaccatgggagctcaggacactctccctgttgcagcagcatttacagaaacagtcaatgcctatttcaaaggagcagacccaagcaaatgtatcgttaagattaccggagaaatggtgttgtcatttcctgctggcatcaccagacactttgccaacaacccgtccccagctgctctgacttttcgggtgataaatttcagcaggttagaacacgtcctgccaaacccccaacttctctgctgtgataatacacaaaatgatgccaataccaaggaattctgggtaaacatgccaaatttgatgactcacctaaagaaagtgtctgaacaaaaaccccaggctacatattataacgttgacatgctcaaatatcaggtgtctgcccagggcattcagtccacacctctgaacctggcagtgaattggcgatgtgagccttcaagcactgacctgcgcatagattacaaatataatacagatgcaatgacgactgctgtggccctcaacaatgtgcagttcctggtccccatcgacggaggagtcaccaagctccaggcagtgctcccaccagcagtctggaatgctgaacaacagagaatattgtggaagattcctgatatctctcagaagtcagaaaatggaggggtgggttctttgttggcaagatttcagttatctgaaggcccaagcaaaccttctccattggttgtgcagttcacaagtgaaggaagcaccctttctggctgtgacattgaacttgttggagcagggtatcgattttcactcatcaagaaaaggtttgctgcaggaaaatacttggcagataactaatgaaatcttatgcaaggatttggaggattcatataatggagaactgatgtatgagaaacagattttaattttggtttgatgaaaacaaaccaatatctgcacttgggatatatcaggtggaaagtcaatgactttcatctgtgatttccctcacacactaccatgatgaccagtcctacagtatttacttctaggtgtaatattgttaatggttttaaaatgtaattattgtatttgtaaattgtactctcattccagtaaggcagttagacacttgagttttagcattttaccattcctgaaatggatgtaatttaaactgtggtatgtaaatttaatagtagtattgttgaatggcacaatgcttacagaggtagattgcattttgtcaatatataaaatttaaatataatattgatagctgtcataaagggggtgccacatattaaagaaacttaagtggaaccagaagaaaaagaaacaaacttacttttcttcaatgcttagtatgttttactctagtgctaaataaaaactctatcttcaaatgtttagtgggttaaattgagaaactatttcagaaaaaaattctaaggttacagcatattcaaagaaaagcattagttaccactttttaaaaagcttttttttcaaactgcaaatttcataaaaatgcaaactgtgtaaacagggcctcttatttttataacttgtgtaaaaagggaaagcaattcatatttaaagtttaagtatattaaattataatcaagagtaaagaagatgttgaagtcttaactacttgcccctctctacagtttcgcaaatgtggggattgctgaataatcagtcagactaaaaccaaaattgtgattttaagatttcaagactttccgtagttgaactggttaagaatttttgcttagttactctgaatagatgatcttactcatccagtatgggggaatgatacctcacgtcttcctctttacccacaggaatcaaaacgctgagactgagaattttagggaaaaaaaagtccactgtttagatccagaaggagagttttaatcattgtttatatcatttgagaatgaaaaaataagcttcataaatgaaattctattcacattactgtgtaataaatttccttttggatgattaggattcattgtataaaactgtaaatctttgccattcttggagaagcaaaaggagagttatcaaaaatgtatgtcgtttcatcgttgcaaggtataataaaaactgtaattattcaatctggccctgccatatgaacatttagaaagacaaacttcttcgggagtctcagttgtaaaaccttccctcattaatatctgaaaatgttagtcttcctttaagtcatagaacttatttaaacataaaccaatttctattacaggttatgctattaaatagctgtaattattaagttattatttttataattagttgttaaatttcattttacacccactcaaatttaacaaagaatctttagcccctttaaattttagaattaaattaaatttttaaagttttacttctaaaatgagattgtgactggcaattgtttatagtgaaactttttaaattaatctttgtactcctctatcagtgcttgctaccaagagaatgtccaaaatgatttgttttaccatgggaaaattcttactattcaacaaactctcagttggccccctacagcagtctggtgttgaagtttctttgaacgaactaaatatactcattttatgtaaaggtatccaatttgattttgaaaccaaaatagaaaatgcaaaattctaaattccatgaaacatggaatttatgacaccaaaatcaatggagagtaagcagcagcaaactgagaattatccagcatatgaatataacaatgtgtttttaagtaatcaattcatttaaaaaattgaatattaatacaaagcatattaaaaacatgtaaatatta
NM_001080397    0,397,490,715,1443,1597,1774,1980,  atgatccccgcagccagcagcaccccgccgggagatgccctcttccccagcgtggccccacaggacttctggaggtcccaggtcacgggctactcggggtccgtgacacgacacctcagtcaccgggccaacaacttcaaacgacaccccaagaggaggaagtgcattcgtccctccccacccccgccccccaacaccccgtgcccgcttgagctggtggacttcggggacctgcacccccagaggtccttccgggagctgcttttcaacggctgcattctctttggcatcgagttcagctacgccatggagacggcgtacgtgaccccggtgctcctgcagatgggcctgcccgaccagctctacagcctggtgtggttcatcagccccatcctcggattcctactgcagcctctgttgggtgcttggagtgaccggtgtacctcaaggtttggaaggagacgccctttcattcttgtcctggctataggggcactgctgggcctctcgctcttgctgaatggccgggacattggcatcgccctggctgacgtgaccgggaaccacaagtggggcctgctgctgaccgtgtgcggtgtggtgctgatggactttagcgccgactcggcggacaaccccagccacgcctacatgatggacgtgtgcagccccgcagaccaggaccgaggcctgaacatccacgccctcctggcaggtctcggaggaggctttggatacgtggtcggcggaatccactgggataaaacgggcttcgggagggccctggggggacagctccgagtcatttacctcttcactgcggtcaccctgagcgtcaccaccgtcctgaccctggtcagcatccctgagaggccgctgcggccgccgagtgagaagcgggcagccatgaagagccccagcctcccgctgcccccgtccccacccgtcctgccagaggaaggccctggcgacagcctcccgtcgcacacggccaccaacttctccagccccatctcgccgcccagccccctcacgcccaagtacggcagcttcatcagcagggacagctccctgacgggcatcagcgagttcgcctcatcctttggcacggccaacatagacagcgtcctcattgactgcttcacgggcggccacgacagctacctggccatccctggcagcgtccccaggccgcccatcagcgtcagcttcccccgggcccccgacggcttctaccgccaggaccgtggacttctggagggcagagagggtgccctgacctccggctgtgacggggacattctgagggtgggctccttggacacctctaagccgaggtcatcagggattctgaagagacctcagaccttggccatcccggacgcagccggaggagggggtcccgaaaccagcaggagaaggaatgtgaccttcagtcagcaggtggccaatatcctgctcaacggcgtgaagtatgagagcgagctgacgggctccagcgagcgcgcggagcagcctctgtccgtggggcgcctctgctccaccatctgcaacatgcccaaggcgctacgcaccctctgcgtcaaccacttcctggggtggctctcattcgaggggatgttgctcttctacacagacttcatgggcgaggtggtgtttcagggggaccccaaggccccgcacacatcagaggcgtatcagaagtacaacagcggcgtgaccatgggctgctggggcatgtgtatctacgccttcagtgctgccttctactcagctatcctggagaagctggaggagttcctcagcgtccgcaccctctacttcatcgcctatctcgccttcggcctggggaccgggcttgccaccctctccaggaacctctacgtggtcctgtcgctctgcataacctacgggattttattttccaccctgtgcaccttgccttactcgctgctctgcgattactatcagagtaagaagtttgcagggtccagtgcggacggcacccggcggggcatgggcgtggacatctctctgctgagctgccagtacttcctggctcagattctggtctccctggtcctggggcccctgacctcggccgtgggcagtgccaacggggtgatgtacttctccagcctcgtgtccttcctgggctgcctgtactcctccctgtttgtcatttatgaaattcctcccagcgacgctgcagacgaggagcaccggcccctcctgctgaacgtctgacatcgcggagcctcgactccggacacgcgcctgcacctgggggtctggagcaggccgaccagtgaggaccaaagggccttgttggacagggggactggctgcctactggaatgtaaatatgtgataaaataataaatgacagcggcaaagccta
NM_001145277    0,182,283,388,470,579,757,  gaaacctggtcagagagtcgcaccgcttccgtccgtcggacagaggaacggtggaagtcgccggaagttcggtgggctccaggcgtcgcgatggaggagagcgggtacgagtcggtgctctgtgtcaagcctgacgtccacgtctaccgcatccctccgcgggctaccaaccgtggctacagggctgcggagtggcagctggaccagccatcatggagtggccggctgaggatcactgcaaagggacagatggcctacatcaagctggaggacaggacgtcaggggagctctttgctcaggccccggtggatcagtttcctggcacagctgtggagagtgtgacggattccagcaggtacttcgtgatccgcatcgaagatggaaatgggcgacgggcgtttattggaattggcttcggggaccgaggtgatgcctttgacttcaatgttgcattgcaggaccatttcaagtgggtgaaacagcagtgtgaatttgcaaaacaagcccagaacccagaccaaggccctaaactggacctgggcttcaaggagggccagaccatcaagctcaacatcgcaaacatgaagaagaaggaaggagcagctgggaatccccgagtccggcctgccagcacaggagggctgagcctgcttccccctcccccaggggggaaaacctccaccctgatccctccccctggggagcagttggctgtggggggatccctcgtccagccagcagttgctcccagttcagatcaacttccagccagacccagccaggcacaggctgggtccagttctgacctgagcacggtttttcctcatgtgacttctgggaaggcgctccctcatctgggccaaaggaaggaggacgaagccctcctcagctggcctgtgtttggggcatgaatctctcctctcctccttgtctggctctgttgacaaaccgggcatgtttggcagtaaattggcaccgtgtcacactgtttcctgggattcaagtatgcaaccagaacacaggagaagaaaagctccaggatccctgtccccatctgtcctcttgatgtgagagagactctgagacttcttccatcgcaatgacctgtattaaacacaagccccccaagcaaaagaagaggttgagtttgctgccaggattcagatcagcccttcccagggtctgcaggtgtcacatgatcacagttcagcgggaggctttccgtacccacactggctgtagccacttcagtccatctgccctccagaggaggggtttcttcctgatttttagcaggtttagaggctgcagcttgagctacaatcaggagggaaattggaaggattagcagcttttaaaaatgtttaaatattttgctttgctaatgtgctgatccgcactaactcatctttgcaaaaggaactgctccctcggcgtgccccagctggggcctctgaagggattcctcactgtgggcagctgccctgagcttcaggcagcagtgtttatctctggccagttgtctggtttccatgtattctaggccaggtaggcaacacagagccaaggcgggtgctggaagccagacggaacagtgttggggcaggaaggtggatgctgttgtcatggagctgtgggagttggcactctgtctgctggtggccctctcggctcacatgttcacagtgcagctcctggcagacttgggttttctctttggtggtttctaaagtgccttatctgcaaacaacttcttttctccttcaggaactgtgaatggctagaagaaggagctcagtaaactagaagtccagggttgcttggtttactggtttataagaaatctgaaagcacctctgacattccttttattaactcacctctcagttgaaagatttcttctttgaaaggtcaagaccgtgaactgaaaaaagtgttggcctttttgcgggaccagatttttaagataaaataaatatttttacttctgtcattgtatgtgaaaaaaaaaaaaa

I'm stack, thanks for your time!!

我是堆叠的,谢谢你的时间!

2 个解决方案

#1


7  

Your input line is ended with a comma, e.g.:

您的输入行以逗号结尾,例如:

0,182,283,388,470,579,757,

So naturally, this will be split into:

很自然,这将分为:

['0', '182', '283', '388', '470', '579', '757', '']

The last element will always be an empty string, ''. You'd need to account for this. One way is simply by ignoring it:

最后一个元素将始终为空字符串''。你需要考虑到这一点。一种方法就是忽略它:

C1 = [float(i) for i in C if i]

Or by cutting out the last element before casting:

或者在铸造之前切掉最后一个元素:

C = row[1].split(",")[1:-1]   # the slice will exclude first and last elements

#2


0  

you could use try/except when converting to float and catch ValueErrors.

转换为float并捕获ValueErrors时,可以使用try / except。

C = row[1].split(",")[1:] 
    for item in C:
    try:
        convert=float(item)
    except ValueError:
        print "not a number"

#1


7  

Your input line is ended with a comma, e.g.:

您的输入行以逗号结尾,例如:

0,182,283,388,470,579,757,

So naturally, this will be split into:

很自然,这将分为:

['0', '182', '283', '388', '470', '579', '757', '']

The last element will always be an empty string, ''. You'd need to account for this. One way is simply by ignoring it:

最后一个元素将始终为空字符串''。你需要考虑到这一点。一种方法就是忽略它:

C1 = [float(i) for i in C if i]

Or by cutting out the last element before casting:

或者在铸造之前切掉最后一个元素:

C = row[1].split(",")[1:-1]   # the slice will exclude first and last elements

#2


0  

you could use try/except when converting to float and catch ValueErrors.

转换为float并捕获ValueErrors时,可以使用try / except。

C = row[1].split(",")[1:] 
    for item in C:
    try:
        convert=float(item)
    except ValueError:
        print "not a number"