Extracting matrix from a text file

T

telekab1

Hello to all!!

I am new in python, and I am running it on Mac with Smultron editor. I
need to read a textfile that includes numbers (in a matrix form),
indexes, and strings, like this:

Marsyas-kea distance matrix for MIREX 2007 Audio Similarity Exchange
Q/R 1 2 3
4 5
1 0 4.54592 4.36685
5.29463 3.85728
2 4.54592 0 3.97667
5.02151 4.64284
3 4.36685 3.97667 0
4.98743 4.83683
4 5.29463 5.02151 4.98743
0 6.04393
5 3.85728 4.64284 4.83683 6.04393 0

My code to get this information is:

matrix = open("dmatrix_5.txt");

while 1:
mat = matrix.readline()
if not mat:
break
pass # do something
print mat



So I just want to keep the matrix in the "middle" for math
computations.

0 4.54592 4.36685
5.29463 3.85728
4.54592 0 3.97667
5.02151 4.64284
4.36685 3.97667 0
4.98743 4.83683
5.29463 5.02151 4.98743
0 6.04393
3.85728 4.64284 4.83683 6.04393 0


I've seen and tried a lot of ways, like split or isinstance.. but
never get the wanted result.... does anyone have an idea, or hint?
Thank you once more for your help!

Best Regards,
Bea
 
M

Mark Lawrence

Hello to all!!

I am new in python, and I am running it on Mac with Smultron editor. I
need to read a textfile that includes numbers (in a matrix form),
indexes, and strings, like this:

Marsyas-kea distance matrix for MIREX 2007 Audio Similarity Exchange
Q/R 1 2 3
4 5
1 0 4.54592 4.36685
5.29463 3.85728
2 4.54592 0 3.97667
5.02151 4.64284
3 4.36685 3.97667 0
4.98743 4.83683
4 5.29463 5.02151 4.98743
0 6.04393
5 3.85728 4.64284 4.83683 6.04393 0

My code to get this information is:

matrix = open("dmatrix_5.txt");

while 1:
mat = matrix.readline()
if not mat:
break
pass # do something
print mat



So I just want to keep the matrix in the "middle" for math
computations.

0 4.54592 4.36685
5.29463 3.85728
4.54592 0 3.97667
5.02151 4.64284
4.36685 3.97667 0
4.98743 4.83683
5.29463 5.02151 4.98743
0 6.04393
3.85728 4.64284 4.83683 6.04393 0


I've seen and tried a lot of ways, like split or isinstance.. but
never get the wanted result.... does anyone have an idea, or hint?
Thank you once more for your help!

Best Regards,
Bea
I think your best bet is to read and action the responses you got to
your original email from three days ago. If these have got lost in the
post simply search online, they're bound to be archived somewhere.
 
C

Colin J. Williams

Hello to all!!

I am new in python, and I am running it on Mac with Smultron editor. I
need to read a textfile that includes numbers (in a matrix form),
indexes, and strings, like this:

Marsyas-kea distance matrix for MIREX 2007 Audio Similarity Exchange
Q/R 1 2 3
4 5
1 0 4.54592 4.36685
5.29463 3.85728
2 4.54592 0 3.97667
5.02151 4.64284
3 4.36685 3.97667 0
4.98743 4.83683
4 5.29463 5.02151 4.98743
0 6.04393
5 3.85728 4.64284 4.83683 6.04393 0

My code to get this information is:

matrix = open("dmatrix_5.txt");

while 1:
mat = matrix.readline()
if not mat:
break
pass # do something
print mat



So I just want to keep the matrix in the "middle" for math
computations.

0 4.54592 4.36685
5.29463 3.85728
4.54592 0 3.97667
5.02151 4.64284
4.36685 3.97667 0
4.98743 4.83683
5.29463 5.02151 4.98743
0 6.04393
3.85728 4.64284 4.83683 6.04393 0


I've seen and tried a lot of ways, like split or isinstance.. but
never get the wanted result.... does anyone have an idea, or hint?
Thank you once more for your help!

Best Regards,
Bea

numpy provides an answer. See the little script below:

import numpy as _n
import string as _s
a= "0 4.54592 4.36685 5.29463 3.85728 " + \
"4.54592 0 3.97667 5.02151 4.64284 " + \
'4.36685 3.97667 0 4.98743 4.83683 ' + \
'5.29463 5.02151 4.98743 0 6.04393 ' + \
'3.85728 4.64284 4.83683 6.04393 0'

d= _n.mat(_n.reshape(_n.array(a.split(), dtype= _n.float), (5, 5)))
print repr(d)
print 'Default printoptions:', _n.get_printoptions()

for p in range(9):
_n.set_printoptions(precision= p)
print 'precision= ', p, '\n', d

When you run this, you'll see that there is a numpy glitch for precisions > 5.

Colin W.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top