When I'm in the mood for C, I usually use the zip and list methods for behavior like scanf. Like this:
input = '1 3.0 false hello' (a, b, c, d) = [t(s) for t,s in zip((int,float,strtobool,str),input.split())] print (a, b, c, d)
Note that for more complex format strings, you need to use regular expressions:
import re input = '1:3.0 false,hello' (a, b, c, d) = [t(s) for t,s in zip((int,float,strtobool,str),re.search('^(\d+):([\d.]+) (\w+),(\w+)$',input).groups())] print (a, b, c, d)
Please note that you need conversion functions for all types that you want to convert. For example, above I used something like:
strtobool = lambda s: {'true': True, 'false': False}[s]
Chris Dellin Jun 18 '12 at 14:55 2012-06-18 14:55
source share