Re: How to convert a string into a list

Discussion in 'Python' started by MRAB, Oct 5, 2010.

  1. MRAB

    MRAB Guest

    On 05/10/2010 02:10, Mark Phillips wrote:
    > I have the following string - "['1', '2']" that I need to convert into a
    > list of integers - [1,2]. The string can contain from 1 to many
    > integers. Eg "['1', '7', '4',......,'n']" (values are not sequential)
    >
    > What would be the best way to do this? I don't want to use eval, as the
    > string is coming from an untrusted source.
    >

    I'd probably use a regex, although others might think it's overkill. :)

    >>> import re
    >>> s = "['1', '2']"
    >>> [int(n) for n in re.findall(r'-?\d+', s)]

    [1, 2]

    An alternative is:

    >>> s = "['1', '2']"
    >>> [int(n.strip("'[] ")) for n in s.split(",")]

    [1, 2]
     
    MRAB, Oct 5, 2010
    #1
    1. Advertising

  2. MRAB <> writes:

    > On 05/10/2010 02:10, Mark Phillips wrote:
    >> I have the following string - "['1', '2']" that I need to convert into a
    >> list of integers - [1,2]. The string can contain from 1 to many
    >> integers. Eg "['1', '7', '4',......,'n']" (values are not sequential)
    >>
    >> What would be the best way to do this? I don't want to use eval, as the
    >> string is coming from an untrusted source.
    >>

    > I'd probably use a regex, although others might think it's overkill. :)
    >
    >>>> import re
    >>>> s = "['1', '2']"
    >>>> [int(n) for n in re.findall(r'-?\d+', s)]

    > [1, 2]
    >
    > An alternative is:
    >
    >>>> s = "['1', '2']"
    >>>> [int(n.strip("'[] ")) for n in s.split(",")]

    > [1, 2]


    I'll add:

    >>> s = ['1', '2', '42']
    >>> [int(x) for x in s.split("'")[1::2]]

    [1, 2, 42]

    --
    Arnaud
     
    Arnaud Delobelle, Oct 5, 2010
    #2
    1. Advertising

  3. MRAB

    Chris Rebert Guest

    On Mon, Oct 4, 2010 at 10:33 PM, Arnaud Delobelle <> wrote:
    > MRAB <> writes:
    >> On 05/10/2010 02:10, Mark Phillips wrote:
    >>> I have the following string - "['1', '2']" that I need to convert into a
    >>> list of integers - [1,2]. The string can contain from 1 to many
    >>> integers. Eg "['1', '7', '4',......,'n']" (values are not sequential)
    >>>
    >>> What would be the best way to do this? I don't want to use eval, as the
    >>> string is coming from an untrusted source.
    >>>

    >> I'd probably use a regex, although others might think it's overkill. :)
    >>
    >>>>> import re
    >>>>> s = "['1', '2']"
    >>>>> [int(n) for n in re.findall(r'-?\d+', s)]

    >> [1, 2]
    >>
    >> An alternative is:
    >>
    >>>>> s = "['1', '2']"
    >>>>> [int(n.strip("'[] ")) for n in s.split(",")]

    >> [1, 2]

    >
    > I'll add:
    >
    >>>> s = ['1', '2', '42']
    >>>> [int(x) for x in s.split("'")[1::2]]

    > [1, 2, 42]


    There's also:
    >>> s = "['1', '2']"
    >>> from ast import literal_eval
    >>> [int(n) for n in literal_eval(s)]

    [1, 2]

    Which is safe, but less strict.

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Oct 5, 2010
    #3
  4. MRAB

    Matteo Landi Guest

    What about using the json library? It could handle errors for you:

    >>>import json
    >>>s = '["1", "2"]'
    >>>json.loads(s)

    [u'1', u'2']

    Now you can convert then to integer values.

    Best regards,
    Matteo


    On Tue, Oct 5, 2010 at 3:41 PM, Mark Phillips
    <> wrote:
    > Thanks to everyone for their suggestions. I learned a lot from them!
    >
    > Mark
    >
    > On Mon, Oct 4, 2010 at 11:54 PM, Chris Rebert <> wrote:
    >>
    >> On Mon, Oct 4, 2010 at 10:33 PM, Arnaud Delobelle <>
    >> wrote:
    >> > MRAB <> writes:
    >> >> On 05/10/2010 02:10, Mark Phillips wrote:
    >> >>> I have the following string - "['1', '2']" that I need to convert into
    >> >>> a
    >> >>> list of integers - [1,2]. The string can contain from 1 to many
    >> >>> integers. Eg "['1', '7', '4',......,'n']" (values are not sequential)
    >> >>>
    >> >>> What would be the best way to do this? I don't want to use eval, as
    >> >>> the
    >> >>> string is coming from an untrusted source.
    >> >>>
    >> >> I'd probably use a regex, although others might think it's overkill.
    >> >> :)
    >> >>
    >> >>>>> import re
    >> >>>>> s = "['1', '2']"
    >> >>>>> [int(n) for n in re.findall(r'-?\d+', s)]
    >> >> [1, 2]
    >> >>
    >> >> An alternative is:
    >> >>
    >> >>>>> s = "['1', '2']"
    >> >>>>> [int(n.strip("'[] ")) for n in s.split(",")]
    >> >> [1, 2]
    >> >
    >> > I'll add:
    >> >
    >> >>>> s = ['1', '2', '42']
    >> >>>> [int(x) for x in s.split("'")[1::2]]
    >> > [1, 2, 42]

    >>
    >> There's also:
    >> >>> s = "['1', '2']"
    >> >>> from ast import literal_eval
    >> >>> [int(n) for n in literal_eval(s)]

    >> [1, 2]
    >>
    >> Which is safe, but less strict.
    >>
    >> Cheers,
    >> Chris
    >> --
    >> http://blog.rebertia.com
    >> --
    >> http://mail.python.org/mailman/listinfo/python-list

    >
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    >




    --
    Matteo Landi
    http://www.matteolandi.net/
     
    Matteo Landi, Oct 5, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Juergen Berchtel
    Replies:
    1
    Views:
    6,031
    John C. Bollinger
    May 20, 2005
  2. Klaus Neuner
    Replies:
    7
    Views:
    505
    Klaus Neuner
    Jul 26, 2004
  3. noydb
    Replies:
    12
    Views:
    350
    noydb
    Aug 19, 2011
  4. news
    Replies:
    2
    Views:
    1,116
  5. Wu Nan
    Replies:
    3
    Views:
    222
    Daniel Berger
    Dec 26, 2007
Loading...

Share This Page