Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.python
Re: string to list when the contents is a list
Vlastimil Brom
2/18/2010 12:09:00 AM
2010/2/18 Wes James <comptekki@gmail.com>:
> 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
> --
>
http://mail.python.org/mailman/listinfo/p...
>
The potentially problematic exec or eval options left aside,
if you really need to do this, you might consider pyparsing; check the example
http://pyparsing.wikispaces.com/file/view/parsePyth...
If you know, the input string will always have this exact format
(single quoted comma separated one-character strings between square
brackets), you might use regular expressions to some extent, e.g.
print re.findall(r"(?<=')\w(?=')", "['a','b','c','b','A']")
['a', 'b', 'c', 'b', 'A']
hth,
vbr
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Re: string to list when the contents is a list
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password