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

Pierre Quentel quentel.pierre at wanadoo.fr
Thu Oct 12 21:32:01 CEST 2006


> >>> parseline( 'A 1 22 3 6', 'sdxf')
> ['A', 1, None, 3.0]

Yes, but in this case the OP expects to get ['A',1,3.0]

A shorter version :

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

Pierre




More information about the Python-list mailing list