Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I can't convert a list of str to a list of floats?

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!!

like image 828
Geparada Avatar asked May 11 '11 01:05

Geparada


People also ask

Why can I not convert string to float?

If you convert a string object into a floating-point in Python many times you will get a ValueError: could not convert string to float. Usually, this happens if the string object has an invalid floating value with spaces or comma Python will throw ValueError while parsing into string object into float.

How do I convert a list of strings to list floats?

The most Pythonic way to convert a list of strings to a list of floats is to use the list comprehension floats = [float(x) for x in strings] . It iterates over all elements in the list and converts each list element x to a float value using the float(x) built-in function.

Can you convert strings to floats?

We can convert a string to float in Python using the float() function. This is a built-in function used to convert an object to a floating point number. Internally, the float() function calls specified object __float__() function.

How do I convert a list of strings to a list of objects?

Pass the List<String> as a parameter to the constructor of a new ArrayList<Object> . List<Object> objectList = new ArrayList<Object>(stringList);


1 Answers

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
like image 158
Santa Avatar answered Oct 28 '22 05:10

Santa