string to list? possible?

Fredrik Lundh fredrik at pythonware.com
Sun Jan 20 16:25:52 EST 2002


"maximilianscherr" wrote:
> i need to convert a string like "[1, 2, 3, 4]" to a list [1, 2, 3, 4].
> possible?

assuming the string always contains a list of integers,
here's one way to do it:

>>> str = "[1, 2, 3, 4]"
>>> import re
>>> map(int, re.findall("\d+", str))
[1, 2, 3, 4]

(if you had something else in mind, you need to start writing
better specifications ;-)

</F>

<!-- (the eff-bot guide to) the python standard library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->







More information about the Python-list mailing list