splitting tables

Karl Pflästerer sigurd at 12move.de
Sun Feb 8 12:09:22 EST 2004


On  8 Feb 2004, robsom <- no.mail at no.mail.it wrote:

> 47.455677 456.67
> 47.4558 453.8
> 47.46789 -9999
> 47.4567 456

> where -9999 (or somethinbg similar) indicates there is a blank, one space
> divides the columns and the elements can have a different number of
> digits. This is of course a worst-case scenario :)
> That is why I used split in the beginning, but then I fall into the other
> problem, when there is a missing value.

I still can't see the problem sorry; if the columns are always separated
by space you can split them with:

>>> s = StringIO.StringIO("""47.455677 456.67
... 47.4558 453.8
... 47.46789 -9999
... 47.4567 456""")
>>> for line in s:
...     print ','.join(map(lambda dat: dat != '-9999' and dat or '', line.split()))
... 
47.455677,456.67
47.4558,453.8
47.46789,
47.4567,456
>>> 
##or
>>> for line in s:
...     print ','.join(map(lambda dat: dat != '-9999' and dat or 'MISSING', line.split()))
... 
47.455677,456.67
47.4558,453.8
47.46789,MISSING
47.4567,456
>>> 


I used here your -9999 as value of a mssing item.  Before you map that
function across the splitted line you culd check for the correct
length to see if you have e.g. like here always two coulmns.  If the
length differs it depends if it's possible to find programmatically the
missing column and just insert a space or something other.



   KP

-- 
 `Beware the Jabberwock, my son!
     The jaws that bite, the claws that catch!
  Beware the Jubjub bird, and shun
    The frumious Bandersnatch!'   "Lewis Carroll" "Jabberwocky"



More information about the Python-list mailing list