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)] >>> line = " 4.5 6.7 23 -1 fred 7.9 " >>> parse_line(line, (float, float, int, int, str, float)) [4.5, 6.7000000000000002, 23, -1, 'fred', 7.9000000000000004] -- Steven. -- http://mail.python.org/mailman/listinfo/python-list