Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Formatting input text file

Reply
Thread Tools

Formatting input text file

 
 
victor.herasme@gmail.com
Guest
Posts: n/a
 
      08-14-2008
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
 
Reply With Quote
 
 
 
 
brad
Guest
Posts: n/a
 
      08-14-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
Gerard flanagan
Guest
Posts: n/a
 
      08-14-2008
(E-Mail Removed) 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'}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Width of text input box vs. password input box cjl HTML 1 10-31-2005 11:46 AM
Off Topic: Width of text input box vs. password input box cjl Javascript 7 10-30-2005 10:11 PM
can i pass data with input text instead of input hidden? Walter ASP General 6 09-17-2004 11:16 AM
why does form with only 1 text input and 1 button input submit on enter? Guy HTML 5 12-13-2003 06:44 AM
Need formatting options menu for formatting hard drive Mark T. Computer Support 3 11-24-2003 11:50 PM



Advertisments