On Mar 6, 2007, at 8:14 AM, Jussi Salmela wrote:
> 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)
> #=======================================
I should have been more specific by saying that all the lines in the
file have the same format. But occasionally I will have a file that
is just not all floats, hence my question for handling a multi-type
line. Since I am the one generating the files I always know which
types the columns in the file have which makes it easier :)
Your answer, together with Steven's has given my much better
understanding of the issue of parsing in tables like this. Thanks
to the both of you!
Cheers
Tommy
--
http://mail.python.org/mailman/listinfo/python-list