convert string containing list to list (or tuple) type

Discussion in 'Python' started by Poppy, May 30, 2008.

  1. Poppy

    Poppy Guest

    I'm reading from a database a column that has a list of codes (comma
    seperated). When I read in the list I have a single value, see code sample
    below values for a, b, and c. These represent possible values in my
    database. I need to loop through each value so I can expand my data from
    this compressed view.

    My code below works and creates my desired output but I believe there must
    be a better way this is very messy. My messy function that I'd like to
    replace is lst_codes(codes). Any alternative suggestions?

    this is what I begin with
    a = ',P,'
    b = ',I,G,AQ,ET,K,BF,'
    c = ',DZ,'
    this is what I want (lists or tuples are fine)
    ['P']
    ['I', 'G', 'AQ', 'ET', 'K', 'BF']
    ['DZ']


    def lst_codes(codes):
    """ turn a string of comma seperated codes into a real list object """
    i = 0
    lstD = []
    while i < len(codes):
    a = codes
    b = ","
    if (i + 1) < len(codes):
    b = codes[i + 1]
    i = i + 1
    else:
    b = ","

    if b <> ",":
    lstD.append(a + b)
    i = i + 2
    else:
    lstD.append(a)
    i = i + 1
    return lstD


    a = ',P,'
    b = ',I,G,AQ,ET,K,BF,'
    c = ',DZ,'

    for ea in (a,b,c):
    print lst_codes(ea.strip(","))
     
    Poppy, May 30, 2008
    #1
    1. Advertising

  2. Poppy

    Poppy Guest

    Arrgh. One of those days where I find an answer just after posting. I spend
    hours on the code below only to find I don't know how to use split to it's
    fullest.

    >>> b.strip(",").split(",")

    ['I', 'G', 'AQ', 'ET', 'K', 'BF']

    "Poppy" <> wrote in message
    news:g1p15p$4jo$...
    > I'm reading from a database a column that has a list of codes (comma
    > seperated). When I read in the list I have a single value, see code sample
    > below values for a, b, and c. These represent possible values in my
    > database. I need to loop through each value so I can expand my data from
    > this compressed view.
    >
    > My code below works and creates my desired output but I believe there must
    > be a better way this is very messy. My messy function that I'd like to
    > replace is lst_codes(codes). Any alternative suggestions?
    >
    > this is what I begin with
    > a = ',P,'
    > b = ',I,G,AQ,ET,K,BF,'
    > c = ',DZ,'
    > this is what I want (lists or tuples are fine)
    > ['P']
    > ['I', 'G', 'AQ', 'ET', 'K', 'BF']
    > ['DZ']
    >
    >
    > def lst_codes(codes):
    > """ turn a string of comma seperated codes into a real list object """
    > i = 0
    > lstD = []
    > while i < len(codes):
    > a = codes
    > b = ","
    > if (i + 1) < len(codes):
    > b = codes[i + 1]
    > i = i + 1
    > else:
    > b = ","
    >
    > if b <> ",":
    > lstD.append(a + b)
    > i = i + 2
    > else:
    > lstD.append(a)
    > i = i + 1
    > return lstD
    >
    >
    > a = ',P,'
    > b = ',I,G,AQ,ET,K,BF,'
    > c = ',DZ,'
    >
    > for ea in (a,b,c):
    > print lst_codes(ea.strip(","))
    >
    >
     
    Poppy, May 30, 2008
    #2
    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. Michal Mikolajczyk
    Replies:
    1
    Views:
    840
    Larry Bates
    Apr 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    1,051
    Jeff Epler
    Apr 20, 2004
  3. Bill Scherer
    Replies:
    0
    Views:
    640
    Bill Scherer
    Apr 20, 2004
  4. Jay Parlar
    Replies:
    0
    Views:
    814
    Jay Parlar
    Mar 17, 2006
  5. Davy
    Replies:
    3
    Views:
    1,917
    Wildemar Wildenburger
    Nov 7, 2007
Loading...

Share This Page