473,664 Members | 2,995 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,B F,'
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,B F,'
c = ',DZ,'

for ea in (a,b,c):
print lst_codes(ea.st rip(","))
Jun 27 '08 #1
2 1285
"Poppy" <zn************ ****@yahoo.comw rote:
a = ',P,'
b = ',I,G,AQ,ET,K,B F,'
c = ',DZ,'

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

ea.strip(',').s plit(',')

?

--
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.comw rote 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,B F,'
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,B F,'
c = ',DZ,'

for ea in (a,b,c):
print lst_codes(ea.st rip(","))


Jun 27 '08 #3

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

Similar topics

5
23832
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 comparisons, averages, stop minus start (elapsed).
16
23005
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
47063
by: cindy liu | last post by:
Hi, In .Net, how to convert a string to a double? Thanks in advance! Cindy
2
2866
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 bin2Date and there also is a Date2bin function of course in cannot find how to do this in csharp. The string looks like this "!$%\0" and should return 03/09/52 can somebody point me in the right direction? Have tried Convert class in several ways...
5
12717
by: XML newbie: Urgent pls help! | last post by:
function to convert string to 1 dimensional array of long in VB.Net
7
4401
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 zero padded (it should always contain 8 chars) and it should display its value in hex form without 0x in the beginning. So if the unsigned long holds the value 0xa87d7d4 the string should contain "0a87d7d4". I tried a combination of iomanip and...
4
2251
by: laxmikiran.bachu | last post by:
Can we have change a unicode string Type object to a Tuple type object.. If so how ????
5
4763
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 call. If some element contains commas, I don't want to split it. Examples: 'foo, bar, baz' ='foo' 'bar' 'baz'
4
5048
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: If today is Sunday, I can set the variable "day" to today by: sunday If I want to find out the day of the week 2 days from now, then this
0
8348
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8861
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8778
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4185
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2764
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1759
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.