Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sbaer
Created April 7, 2013 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbaer/5331496 to your computer and use it in GitHub Desktop.
Save sbaer/5331496 to your computer and use it in GitHub Desktop.
Parse a string into a list of numbers
from itertools import *
def tonumberlist(s):
rc = []
def shouldtake(x):
return str.isdigit(x) or x=='.'
i = iter(s.replace(' ',''))
while True:
try:
rc.append(float(''.join(takewhile(shouldtake, i))))
except ValueError: break
return rc
s = "12.1, 5; 66.32,123"
print tonumberlist(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment