paseline(my favorite simple script): does something similar exist?

Pierre Quentel quentel.pierre at wanadoo.fr
Thu Oct 12 15:25:21 EDT 2006


Hi Rick,

Nice little script indeed !

You probably mean
>         trans = xlat.get(f,None)
instead of
>         trans = xlat.get(f,'None')

in the case where an invalid format character is supplied. The string
'None' evaluates to True, so that trans(words[i]) raises an exception

A variant, with a list comprehension instead of the for loop :

def parseline(line,format):
    xlat = {'x':None,'s':str,'f':float,'d':int,'i':int}
    result = []
    words = line.split()
    result = [ xlat[f](w) for f,w in zip(format,words)
        if xlat.get(f,None) ]
    if not result: return None
    if len(result) == 1: return result[0]
    return result

Regards,
Pierre




More information about the Python-list mailing list