473,287 Members | 1,663 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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(","))
Jun 27 '08 #1
2 1269
"Poppy" <zn****************@yahoo.comwrote:
a = ',P,'
b = ',I,G,AQ,ET,K,BF,'
c = ',DZ,'

for ea in (a,b,c):
print lst_codes(ea.strip(","))
Why not just use:

ea.strip(',').split(',')

?

--
Duncan Booth http://kupuguy.blogspot.com
Jun 27 '08 #2
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" <zn****************@yahoo.comwrote in message
news:g1**********@aioe.org...
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(","))


Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Hank | last post by:
hi, i have a string as follows 18Nov2003:18:23:43:405 Is there an easy way to convert that to absolute time? What i really want to do is to parse these times from a log file and do time...
16
by: flyaflya | last post by:
a = "(1,2,3)" I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', '2', ',', '3', ')') not (1,2,3)
4
by: cindy liu | last post by:
Hi, In .Net, how to convert a string to a double? Thanks in advance! Cindy
2
by: Remi Caron | last post by:
Hi, I took over an Visual Object project (Visual Clipper) in that language there is a function to: Convert a string containing a 32-bit binary date to a date data type. That function is called...
5
by: XML newbie: Urgent pls help! | last post by:
function to convert string to 1 dimensional array of long in VB.Net
7
by: Eric Lilja | last post by:
Hello, I have an unsigned long that I need to convert to a std::string. The unsigned long holds 32-bit checksums and sometimes the most significant byte is 0 and in those cases the string should be...
4
by: laxmikiran.bachu | last post by:
Can we have change a unicode string Type object to a Tuple type object.. If so how ????
5
by: Robert Dodier | last post by:
Hello, I'd like to split a string by commas, but only at the "top level" so to speak. An element can be a comma-less substring, or a quoted string, or a substring which looks like a function...
4
by: Samir | last post by:
Is there a way to loop or iterate through a list/tuple in such a way that when you reach the end, you start over at the beginning? For example, suppose I define a list "daysOfWeek" such that: ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.