Extracting matrix from a text file

Discussion in 'Python' started by bbarbero@inescporto.pt, Aug 7, 2009.

  1. Guest

    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



    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

    ----------------------------------------------------------------
    This message was sent using IMP, the Internet Messaging Program.
    , Aug 7, 2009
    #1
    1. Advertising

  2. alex23 Guest

    On Aug 8, 2:19 am, wrote:
    > 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
    >
    > 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!



    isinstance? Are you just randomly trying functions hoping they'll
    work? :)

    Untested code follows:

    with open(<textfile>,'r') as textfile:
    header = textfile.next() # skip the header
    col_0_size = 8 # cos it does
    for line in textfile:
    newline = line[col_0_size:] # strip off the index column
    columns = newline.split(' ') # will give you a tuple of
    strings
    one, two, three, four, five = map(float, columns) # turns the
    strings into floats
    # do whatever you want to those values here

    This is fairly standard text handling with Python, if you haven't
    already you should really work through the Python tutorial[1],
    especially the section on strings [2], and if you have, perhaps David
    Mertz's 'Text Processing in Python'[3] may be of use.

    1: http://docs.python.org/tutorial/
    2: http://docs.python.org/tutorial/introduction.html#strings
    3: http://gnosis.cx/TPiP/
    alex23, Aug 7, 2009
    #2
    1. Advertising

  3. MRAB Guest

    alex23 wrote:
    > On Aug 8, 2:19 am, wrote:
    >> 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
    >>
    >> 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!

    >
    >
    > isinstance? Are you just randomly trying functions hoping they'll
    > work? :)
    >
    > Untested code follows:
    >
    > with open(<textfile>,'r') as textfile:
    > header = textfile.next() # skip the header
    > col_0_size = 8 # cos it does
    > for line in textfile:
    > newline = line[col_0_size:] # strip off the index column
    > columns = newline.split(' ') # will give you a tuple of
    > strings

    [snip]
    Or:
    columns = line.split(' ')[1 : ]
    MRAB, Aug 7, 2009
    #3
  4. alex23 Guest

    MRAB <> wrote:
    > Or:
    >          columns = line.split(' ')[1 : ]


    Even better, well spotted.
    alex23, Aug 7, 2009
    #4
    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. lvcargnini

    Matrix composed by two matrix

    lvcargnini, Jul 4, 2006, in forum: VHDL
    Replies:
    3
    Views:
    2,646
    Jonathan Bromley
    Jul 5, 2006
  2. Holgerson

    Matrix*Vector and Vector*Matrix

    Holgerson, Oct 25, 2007, in forum: C++
    Replies:
    3
    Views:
    397
    Holgerson
    Oct 26, 2007
  3. Terry Reedy
    Replies:
    0
    Views:
    542
    Terry Reedy
    Apr 2, 2009
  4. Robert Kern
    Replies:
    0
    Views:
    581
    Robert Kern
    Apr 2, 2009
  5. Replies:
    2
    Views:
    321
    Colin J. Williams
    Aug 11, 2009
Loading...

Share This Page