1

how can I convert a string to an int in python say I have this array

['(111,11,12)','(12,34,56)'] to [(111,11,12),(12,34,56)]

Any help will be appreciated thanks

1
  • @closevoter: I don't see any reason why this should be too localized though you may choose to vote it as a duplicate Commented Dec 27, 2012 at 14:52

5 Answers 5

8
import ast
a = "['(111,11,12)','(12,34,56)']"
[ast.literal_eval(b) for b in ast.literal_eval(a)]
# [(111, 11, 12), (12, 34, 56)]

EDIT: if you have a list of strings (and not a string), just like @DSM suggests, then you have to modify it:

a = ['(111,11,12)','(12,34,56)']
[ast.literal_eval(b) for b in a]
# [(111, 11, 12), (12, 34, 56)]
Sign up to request clarification or add additional context in comments.

2 Comments

The OP seems to have a list of strings, not a string (the modification is trivial, of course.)
@Goranek ast.literal_eval is different from the built-in eval - it is completely safe to use even when dealing with data from untrusted sources. See docs.python.org/2/library/ast.html#ast-helpers for more info.
0

You could try some re:

import re
src = ['(111,11,12)', '(12,34,56)']
[tuple([int(n) for n in re.findall(r"(\d+),(\d+),(\d+)", s)[0]]) for s in src]

Comments

0

By reading your question I see you have a list of strings:

l = ['(111,11,12)','(12,34,56)']

and you want to convert that to a list of numbers...

# some cleaning first
number_list = [x.strip('()').split(',') for x in l]
for numbers in number_list:
    numbers[:] = [int(x) for x in numbers]
print number_list

Sorry for that list comprehension parsing, if you're new that looks weird but is a very common python idiom and you should get familiar with it.

Comments

0

Have fun!

def customIntparser(n):
    exec("n="+str(n))
    if type(n) is list or type(n) is tuple:
        temps=[]
        for a in n:
            temps.append(customIntparser(str(a)))
        if type(n) is tuple:
            temps=tuple(temps)
        return temps
    else:
        exec("z="+str(n))
        return z

Sample tests:

>>>s = "['(111,11,12)','(12,34,56)']"
>>>a=customIntparser(s)
>>> a
# [(111, 11, 12), (12, 34, 56)]
>a[0][1]
# 11

Comments

-2

You can convert a string to an int with the int() keyword:

Python 2.7.2 (default, Jun 20 2012, 16:23:33) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> int('42')
42

But the example you give seems to indicate that you want to do this for an entire tuple, not a single integer. If so, you can use the builtin eval function:

>>> eval('(111,111)')
(111, 111)

1 Comment

-1: Your answer is least helpful to the OP's question and please don't suggest the dreaded eval.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.