convert string containing list to list (or tuple) type



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[i]
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(","))


.



Relevant Pages

  • Re: Getting a list of list permissions for WSS
    ... this on a backup copy of the database so you're not hitting the live DB. ... Now when you run the script and refresh the SPS Event List which this ... my departmental Event Lists roll-up flawlessly to a single Portal list. ...
    (microsoft.public.sharepoint.portalserver)
  • Re: The Sorrows of Record Cataloguing
    ... but the database ... needed was a list so that I wouldn't buy records or CDs over again. ... to see on the shelves and were in alpha-order by composer. ... recordings, there is an entirely different set of lists -- five lists, ...
    (rec.music.classical.recordings)
  • Re: Are you one of the EIGHT MILLION who will be arrested when Bush declares martial law in October?
    ... Called "Main Core," the database's origins date back to the 1980s when ... "There exists a database ... of Americans, who, often for the slightest and most trivial reason, ... called someone on those lists, you get added to the master list. ...
    (alt.politics)
  • www.CeBeans.com - new program listings - Dec 17 2007
    ... This program is a database of 24 Christmas/Holdiay tips for a safe season. ... This program is a database of 24 cooking email lists that you can subscribe ... The object of this game is to use your taser gun to shoot down the birds ... Free man every round after five. ...
    (microsoft.public.pocketpc)
  • Re: convert string containing list to list (or tuple) type
    ... One of those days where I find an answer just after posting. ... My messy function that I'd like to ... this is what I want (lists or tuples are fine) ... return lstD ...
    (comp.lang.python)