Formatting input text file

Gerard flanagan grflanagan at gmail.com
Thu Aug 14 13:50:20 EDT 2008


victor.herasme at gmail.com wrote:
> Hi,
> 
> it's me again with tons of questions. I hava an input file structured
> like this:
> 
>                   X               XYData-1
> 
>                 1.                 3.08333
>                 2.                 9.05526
>                 3.                 3.13581
>                 .......
> 
>                   X               XYData-2
> 
>                 1.                 4.08322
>                 2.                 4.02526
>                 3.                 3.95891
>                 ...............
> 
> i want to format it so i only get the second column, in order to place
> it in a mxn matrix. Let's say i want this:
> 
> number1   number2   number3
> number4   number5   number6
> 



def iter_data(fileobj, numcols=3):
     i = 0
     keys = [str(j) for j in range(1, numcols+1)]
     for line in fileobj:
         if not line.strip():
             continue
         parts = line.split()
         if len(parts) != 2:
             continue
         key, val = parts[0].strip()[:-1], parts[1].strip()
         if key in keys:
             i = (i % numcols) + 1
             while key > str(i):
                 yield None
                 i = (i % numcols) + 1
             yield val
         elif i and val.startswith('XYData-'):
             # reset index for a new data row, also padding previous row
             while i < numcols:
                 yield None
                 i += 1

def iter_coords(numcols):
     row = 0
     while True:
         yield divmod(row, numcols)
         row += 1

from itertools import izip
from StringIO import StringIO

data = '''
                   X               XYData-1

                 1.                 3.08333
                 2.                 9.05526
                 3.                 3.13581

                   X               XYData-2

                 2.                 4.02526
                 3.                 3.95891

                   X               XYData-3

                 1.                 4.08322
                 2.                 3.95891

                   X               XYData-4

                 1.                 3.08333
                 3.                 3.13581

                   X               XYData-5

                 1.                 3.08333
                 2.                 9.05526
                 4.                 3.13581
                 5.                 3.13581
'''

print
buf = StringIO(data)
k = 0
for item in iter_data(buf, 5):
     if k % 5 == 0:
         print
     print '|   %s%s' % (item, ' ' * (8-len(str(item)))),
     k += 1

print
buf = StringIO(data)
for item in izip(iter_coords(5), iter_data(buf, 5)):
     print item

print
buf = StringIO(data)
d = dict(izip(iter_coords(5), iter_data(buf, 5)))

print d

++++++++++++++++++++++++++++++++++++++++++++++



|   3.08333  |   9.05526  |   3.13581  |   None     |   None
|   None     |   4.02526  |   3.95891  |   None     |   None
|   4.08322  |   3.95891  |   None     |   None     |   None
|   3.08333  |   None     |   3.13581  |   None     |   None
|   3.08333  |   9.05526  |   None     |   3.13581  |   3.13581
((0, 0), '3.08333')
((0, 1), '9.05526')
((0, 2), '3.13581')
((0, 3), None)
((0, 4), None)
((1, 0), None)
((1, 1), '4.02526')
((1, 2), '3.95891')
((1, 3), None)
((1, 4), None)
((2, 0), '4.08322')
((2, 1), '3.95891')
((2, 2), None)
((2, 3), None)
((2, 4), None)
((3, 0), '3.08333')
((3, 1), None)
((3, 2), '3.13581')
((3, 3), None)
((3, 4), None)
((4, 0), '3.08333')
((4, 1), '9.05526')
((4, 2), None)
((4, 3), '3.13581')
((4, 4), '3.13581')

{(1, 3): None, (3, 0): '3.08333', (2, 1): '3.95891', (0, 3): None, (4, 
0): '3.08333', (1, 2): '3.95891', (3, 3): None, (4, 4): '3.13581', (2, 
2): None, (4, 1): '9.05526', (1, 1): '4.02526', (3, 2): '3.13581', (0, 
0): '3.08333', (0, 4): None, (1, 4): None, (2, 3): None, (4, 2): None, 
(1, 0): None, (0, 1): '9.05526', (3, 1): None, (2, 4): None, (2, 0): 
'4.08322', (4, 3): '3.13581', (3, 4): None, (0, 2): '3.13581'}




More information about the Python-list mailing list