Good handling of input data (was Re: Newbie question)

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Tue Mar 6 14:50:11 CET 2007


Steven D'Aprano a écrit :
> On Tue, 06 Mar 2007 05:57:43 -0500, Tommy Grav wrote:
> 
> 
>>So how would you handle this type of error handling?
>>My main problem is that occasionally there is an entry
>>in the list that is a string:
>>
>>0.9834 134.4933 78.009 run11 27
> 
> 
> How do you want to deal with such an occasional string? What do you expect
> the code to do?
> 
> The way I see it, if you're expecting five floats, and you get four floats
> and a string, that's an error. Do you ignore the entire line, or replace
> the string with zero, or what?
> 
> 
> 
>>Again I would like to avoid having to individually parse the 3 floats,
>>while still easily handling the string, but a list comprehension will
>>not work as far as I can tell. Is there a module that handles this
>>type of flat ascii tables? Something like:
>>
>>(x,y,z,id,n) = ParseFile("float","float","float","string","int")
>>
>>would be great, and I guess
>>
>>(x,y,z,id,n) = PaseFile2("%f %f %f %s %d")
>>
>>would be even better.
> 
> 
> How about this?
> 
> def parse_line(line, types):
>     items = line.split()
>     return [t(s) for (t, s) in zip(types, items)]
> 

Since the OP mention of last items being here "occasionnaly", I'd go for 
this instead (nb: ad-hoc implementation - yours is of course better as a 
general solution):

def parse_line(line, *types):
     items = line.split()
     return [t(s) for (s, t) in zip(items, types)]


My 2 cents



More information about the Python-list mailing list