string to list when the contents is a list

Discussion in 'Python' started by Wes James, Feb 17, 2010.

  1. Wes James

    Wes James Guest

    I have been trying to create a list form a string. The string will be
    a list (this is the contents will look like a list). i.e. "[]" or
    "['a','b']"

    The "[]" is simple since I can just check if value == "[]" then return []

    But with "['a','b']" I have tried and get:

    a="['a','b']"

    b=a[1:-1].split(',')

    returns

    [ " 'a' "," 'b' " ]

    when I want it to return ['a','b'].

    How can I do this?

    thx,

    -wes
     
    Wes James, Feb 17, 2010
    #1
    1. Advertisements

  2. Wes James

    nn Guest

    Wes James wrote:
    > I have been trying to create a list form a string. The string will be
    > a list (this is the contents will look like a list). i.e. "[]" or
    > "['a','b']"
    >
    > The "[]" is simple since I can just check if value == "[]" then return []
    >
    > But with "['a','b']" I have tried and get:
    >
    > a="['a','b']"
    >
    > b=a[1:-1].split(',')
    >
    > returns
    >
    > [ " 'a' "," 'b' " ]
    >
    > when I want it to return ['a','b'].
    >
    > How can I do this?
    >
    > thx,
    >
    > -wes



    I am surprised nobody gave you the simple answer yet that may even
    work for your situation:

    b=a[2:-2].split("','")
     
    nn, Feb 18, 2010
    #2
    1. Advertisements

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. tony collier
    Replies:
    1
    Views:
    490
    Dan Brussee
    Jan 11, 2004
  2. Ken Cox [Microsoft MVP]

    Re: Assigning contents within HTML tag to string variable.

    Ken Cox [Microsoft MVP], Aug 5, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    983
    tonymjohn
    Aug 7, 2004
  3. Don Adams
    Replies:
    1
    Views:
    778
    Martin Honnen
    Mar 5, 2004
  4. feraso
    Replies:
    0
    Views:
    572
    feraso
    Apr 12, 2009
  5. Angus
    Replies:
    6
    Views:
    2,142
    James Kanze
    Jan 23, 2010
  6. mallyonline
    Replies:
    3
    Views:
    262
    Bob Lehmann
    Oct 12, 2005
  7. Kamarulnizam Rahim
    Replies:
    4
    Views:
    436
    Robert Klemme
    Jan 28, 2011
  8. Replies:
    0
    Views:
    370
Loading...