Convert int to string in python


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert int to string in python
# 1  
Old 01-06-2014
Convert int to string in python

Hi,

I have column 5 in a file which contains string like this for ex.
Code:
RP11-125O5.2
SLCO1B1
CAPN1
FRMPD2
TXNL4B

So I do by
Code:
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

Last edited by vbe; 01-06-2014 at 10:34 AM.. Reason: code tags
# 2  
Old 01-06-2014
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?
# 3  
Old 01-06-2014
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...

Last edited by wisecracker; 01-06-2014 at 11:57 AM.. Reason: See above...
# 4  
Old 01-06-2014
Hi Wisecarcker :

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

Python version 2.7
OS linux
# 5  
Old 01-06-2014
Quote:
Originally Posted by rossi
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?
# 6  
Old 01-06-2014
My data is like this :

Code:
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:
Code:
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

Last edited by bartus11; 01-06-2014 at 01:11 PM.. Reason: Please use CODE tags instead of ICODE, for multiline output.
# 7  
Old 01-06-2014
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Splitting string and storing in int

I have a string containing 2 integers separated by /, for example 12/8 or 8/6 am want to store the numbers in two integers. (3 Replies)
Discussion started by: kristinu
3 Replies

2. Programming

IPv4 string->int

Does anyone know how to convert a IP address given as 'string' into a 'u_int32_t'? Are there any build any functions already? (1 Reply)
Discussion started by: Freaky123
1 Replies

3. Shell Programming and Scripting

Bash shell script: Str(007) to int(7),increment it(8) & convert back to string(008)

Hi, I have the following requirement. There will be following text/line in a file (eg: search-build.txt) PRODUCT_VERSION="V:01.002.007.Build1234" I need to update the incremental build number (eg here 007) every time I give a build through script. I am able to search the string and get... (4 Replies)
Discussion started by: drwatson_droid
4 Replies

4. Programming

How to pass int and return string in C?

hi I want to write a function which takes int as input and returns a string like this. char GetString(int iNo) { switch(iNo) { case 0: return "Zero"; break; case 1: return "One"; break; } } void main() { int i; printf("Enter... (1 Reply)
Discussion started by: atharalikhan
1 Replies

5. Programming

Handle int listen(int sockfd, int backlog) in TCP

Hi, from the manual listen(2): listen for connections on socket - Linux man page It has a parameter called backlog and it limits the maximum length of queue of pending list. If I set backlog to 128, is it means no more than 128 packets can be handled by server? If I have three... (3 Replies)
Discussion started by: sehang
3 Replies

6. Shell Programming and Scripting

From string to int ?

hello guys i m new to shell scripting and can't find out why this structure is not right I m guessing this happens because $LINESUM is a string . so how can i do this ? i want my script to do so many loops as the number of the lines of one custom file. #!/bin/bash echo give me path name... (5 Replies)
Discussion started by: xamxam
5 Replies

7. Shell Programming and Scripting

Compare string output to int value?

Hi, i'd like to implmeent emergency shutdown script in case our AC dies and the temperature rises too high. I can get core temperatures with: sensors | grep Core | cut -c15-16 Result is: 23 18 18 13 21 18 15 17 How can I check if any of the cores is above eg. 80 (that's C of... (2 Replies)
Discussion started by: zapp0
2 Replies

8. Solaris

Convert DWORD to int on solaris for 64-bit

i am converting application from 32-bit to 64-bit on Solaris. How can I convert "DWORD" on Solaris 64-bit to int. Thanks. (1 Reply)
Discussion started by: amit_27
1 Replies

9. Programming

to convert int to hex

Hi, Can you help me in converting int value to hex in a single command. Thanks (8 Replies)
Discussion started by: naan
8 Replies

10. Shell Programming and Scripting

how to convert a string to int

Hi, i want to read a text file whose content(single line) will be a number like 1 or 2 or 3 ..... what i want to do is to read the file and increment the content of the file, using unix scripting. Regards, Senthil Kumar Siddhan. (2 Replies)
Discussion started by: senthilk615
2 Replies
Login or Register to Ask a Question