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

Neil Cerutti horpner at yahoo.com
Thu Oct 12 21:34:07 CEST 2006


On 2006-10-12, Pierre Quentel <quentel.pierre at wanadoo.fr> wrote:
>> >>> 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

I don't like the name, since it actually seems to be parsing a
string.

-- 
Neil Cerutti



More information about the Python-list mailing list