How to parse a string completely into a list

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Thu Sep 25 04:08:09 EDT 2008


john.ford at colorado.edu a écrit :
> I want to take a long alpha-numeric string with \n and white-space and
> place ALL elements of the string (even individual parts of a long
> white-space) into separate list elements. The most common way I've
> seen this performed is with the split() function, however I don't
> believe that it has the power to do what I am looking for.
> Any suggestions?

Did you try passing your string to the list() type ?

Python 2.5.1 (r251:54863, Mar  7 2008, 03:41:45)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> s = """I want to take a long alpha-numeric string with \n and 
white-space and
... place ALL elements of the string (even individual parts of a long
... white-space) into separate list elements. The most common way I've
... seen this performed is with the split() function, however I don't
... believe that it has the power to do what I am looking for.
... Any suggestions?
... thanks
... """
 >>>
 >>> s
"I want to take a long alpha-numeric string with \n and white-space 
and\nplace ALL elements of the string (even individual parts of a 
long\nwhite-space) into separate list elements. The most common way 
I've\nseen this performed is with the split() function, however I 
don't\nbelieve that it has the power to do what I am looking for.\nAny 
suggestions?\nthanks\n"
 >>> list(s)
['I', ' ', 'w', 'a', 'n', 't', ' ', 't', 'o', ' ', 't', 'a', 'k', 'e', ' 
', 'a', ' ', 'l', 'o', 'n', 'g', ' ', 'a', 'l', 'p', 'h', 'a', '-', 'n', 
'u', 'm', 'e', 'r', 'i', 'c', ' ', 's', 't', 'r', 'i', 'n', 'g', ' ', 
'w', 'i', 't', 'h', ' ', '\n', ' ', 'a', 'n', 'd', ' ', 'w', 'h', 'i', 
't', 'e', '-', 's', 'p', 'a', 'c', 'e', ' ', 'a', 'n', 'd', '\n', 'p', 
'l', 'a', 'c', 'e', ' ', 'A', 'L', 'L', ' ', 'e', 'l', 'e', 'm', 'e', 
'n', 't', 's', ' ', 'o', 'f', ' ', 't', 'h', 'e', ' ', 's', 't', 'r', 
'i', 'n', 'g', ' ', '(', 'e', 'v', 'e', 'n', ' ', 'i', 'n', 'd', 'i', 
'v', 'i', 'd', 'u', 'a', 'l', ' ', 'p', 'a', 'r', 't', 's', ' ', 'o', 
'f', ' ', 'a', ' ', 'l', 'o', 'n', 'g', '\n', 'w', 'h', 'i', 't', 'e', 
'-', 's', 'p', 'a', 'c', 'e', ')', ' ', 'i', 'n', 't', 'o', ' ', 's', 
'e', 'p', 'a', 'r', 'a', 't', 'e', ' ', 'l', 'i', 's', 't', ' ', 'e', 
'l', 'e', 'm', 'e', 'n', 't', 's', '.', ' ', 'T', 'h', 'e', ' ', 'm', 
'o', 's', 't', ' ', 'c', 'o', 'm', 'm', 'o', 'n', ' ', 'w', 'a', 'y', ' 
', 'I', "'", 'v', 'e', '\n', 's', 'e', 'e', 'n', ' ', 't', 'h', 'i', 
's', ' ', 'p', 'e', 'r', 'f', 'o', 'r', 'm', 'e', 'd', ' ', 'i', 's', ' 
', 'w', 'i', 't', 'h', ' ', 't', 'h', 'e', ' ', 's', 'p', 'l', 'i', 't', 
'(', ')', ' ', 'f', 'u', 'n', 'c', 't', 'i', 'o', 'n', ',', ' ', 'h', 
'o', 'w', 'e', 'v', 'e', 'r', ' ', 'I', ' ', 'd', 'o', 'n', "'", 't', 
'\n', 'b', 'e', 'l', 'i', 'e', 'v', 'e', ' ', 't', 'h', 'a', 't', ' ', 
'i', 't', ' ', 'h', 'a', 's', ' ', 't', 'h', 'e', ' ', 'p', 'o', 'w', 
'e', 'r', ' ', 't', 'o', ' ', 'd', 'o', ' ', 'w', 'h', 'a', 't', ' ', 
'I', ' ', 'a', 'm', ' ', 'l', 'o', 'o', 'k', 'i', 'n', 'g', ' ', 'f', 
'o', 'r', '.', '\n', 'A', 'n', 'y', ' ', 's', 'u', 'g', 'g', 'e', 's', 
't', 'i', 'o', 'n', 's', '?', '\n', 't', 'h', 'a', 'n', 'k', 's', '\n']
 >>>


HTH



More information about the Python-list mailing list