Convert int to string in python

Hi,

I have column 5 in a file which contains string like this for ex.

RP11-125O5.2
SLCO1B1
CAPN1
FRMPD2
TXNL4B

So I do by

data = [map(int,cols[5])]
ValueError: invalid literal for int() with base 10: 'R'

Can someday tell me how to convert this column into int successfully.

Thank You in advance.

Regards
Rossi

To state the obvious, the values are not integers and some have multiple groups of digits and so creation of the required integer requires a more precise specification.

Could you provide the integer values you want returned for the above?

Or do you want to catch the exception and handle these invalid columns differently?

I am at a loss here.
Is it a string representation of the _numbers_ part?
If so then does that include the "." which makes that part a float?
What about the "-", (minus in this case; NOT hyphen), is that needed?
OR...
Is it a numerical representation of each ASCII character, "ord()" for example.
Lastly your <title> is the opposite of what your <text body> says.
Which is it?

Please give us more to go on...

Python version, machine, OS, etc...

EDIT:
BTW Python has had a wonderful traceback system ever since I first started exerimenting with it at version 1.4.0...

Hi Wisecarcker :

I want to convert the whole column into int. all alphanumerical characters in string.

Python version 2.7
OS linux

there's no need to convert to int unless you want to do some mathematics on those columns. Your column contains letters. What are you actually trying to do with that column?

My data is like this :

chr12   21007916        21377807        369.89  -3.12   RP11-125O5.2
chr12   21007916        21377807        369.89  -3.12   SLCO1B1
chr12   21007916        21377807        369.89  -3.12   SLCO1B3
chr12   21007916        21377807        369.89  -3.12   RP11-545J16.1
chr12   21007916        21377807        369.89  -3.12   SLCO1B7
chr12   6625994 6627160 1.17    -5.01   NCAPD2
chr11   64977257        64981527        4.27    -6.72   CAPN1
chr11   64977257        64981527        4.27    -6.72   CAPN1

I need an output:

chr12   21007916        21377807        369.89  -3.12    RP11-125O5.2,SLCO1B1,SLCO1B7
chr12   6625994 6627160 1.17    -5.01   NCAPD2
chr11   64977257        64981527        4.27    -6.72   CAPN1

I have made a script but need to convert the column 5 into int in order to make it working

No, you don't want to convert column 5 to int, you want to group records with identical fields 0-4 and join their field 5 values with a comma.

Given the modified spec could you try again and let us know how you get on

HI all,

I solved the problem in a different

from collections import defaultdict
import sys

d = defaultdict(list)

with open (sys.argv[1]) as f:
        for line in f:
                line_data = line.strip().split()
                d[' '.join(line_data[0:5])].append(line_data[5])

for k in sorted(d.keys(), key = lambda s: (s.split()[0], int(s.split()[1]))):
        print(k,  ', '.join(d[k]))