Good handling of input data (was Re: Newbie question)
Jussi Salmela
tiedon_jano at hotmail.com
Tue Mar 6 08:14:30 EST 2007
Tommy Grav kirjoitti:
> <snip>
> Thanks for the great response.
>
> 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
>
> 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.
>
> Cheers
> Tommy
>
>
>
Being just on the first step of the ladder of total clairvoyance, I
can't disambiguate the word "occasionally". I'm assuming that you have
two types of rows: the ones with 3 floats and the ones with 3 floats, a
string and an integer.
Here's a possible solution:
#=======================================
conv = {'f':float, 'i':int, 's':str}
lines = ['0.3434 0.5322 0.3345\n',
'0.9834 134.4933 78.009 run11 27\n',
'1.3435 2.3345 5.3433\n']
for line in lines:
line = line.split()
if len(line) == 3:
pat = 'fff'
(x, y, z) = [conv[p](x) for p,x in zip(pat, line)]
print x, y, z
print type(x), type(y), type(z)
else:
pat = 'fffsi'
(x, y, z, s, i) = [conv[p](x) for p,x in zip(pat, line)]
print x, y, z, s, i
print type(x), type(y), type(z), type(s), type(i)
#=======================================
HTH,
Jussi
More information about the Python-list
mailing list