Formatting input text file

V

victor.herasme

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
 
B

brad

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.
 
G

Gerard flanagan

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'}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top