Formatting input text file

Discussion in 'Python' started by victor.herasme@gmail.com, Aug 14, 2008.

  1. Guest

    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

    i hope it is not too hard to do. Any help greatly apreciated. Thanks,

    Victor
    , Aug 14, 2008
    #1
    1. Advertising

  2. brad Guest

    wrote:
    > Hi,
    >
    > it's me again with tons of questions. I hava an input file structured
    > like this:
    >
    > X XYData-1
    >
    > 1. 3.08333
    >
    > number1 number2 number3
    > number4 number5 number6


    split is your friend.
    brad, Aug 14, 2008
    #2
    1. Advertising

  3. 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'}
    Gerard flanagan, Aug 14, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guy
    Replies:
    5
    Views:
    2,824
    brucie
    Dec 13, 2003
  2. cjl
    Replies:
    1
    Views:
    10,497
    Michael Winter
    Oct 31, 2005
  3. Tom
    Replies:
    0
    Views:
    332
  4. Walter
    Replies:
    6
    Views:
    237
    Bob Barrows [MVP]
    Sep 17, 2004
  5. cjl
    Replies:
    7
    Views:
    179
    Thomas 'PointedEars' Lahn
    Oct 30, 2005
Loading...

Share This Page