#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Reading text file containing square matrix


    Hi everyone,

    I am a beginner at python and so far it's been great learning this language. I'm currently working on an assignment which is asking me to do some operations on a multidimensional square matrix. My problem is getting the matrix out of a text file because I just can't figure out how to get it out. I can print the matrix but I can never do anything to it like find its dot product or transpose.

    My code so far...
    Code:
    from numpy import *
    
    textfile=raw_input('What is your textfile name? (Refrain from putting .txt):')
    print 'You have entered', textfile+'.txt'
    new_copy=[]
    filename=open(textfile+'.txt')
    for line in filename:
    	new_copy.append(line)
    print new_copy
    Any help would be gladly appreciated!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481
    A solution (without matrix product)
    Code:
    import pprint
    
    textfile=raw_input('What is your textfile name? (Refrain from putting .txt):')
    name = textfile + '.txt'
    print 'loading',name
    
    with open(name,'r') as inf:
    	text_data = inf.readlines()
    
    table = []
    for Line in text_data:
        row = []
        for field in Line.strip().split():
            row.append(float(field))          # convert to float
        table.append(row)
    
    print('the table is:')
    pprint.pprint(table)
    
    def transpose(A):
        return list(zip(*A))
    
    print('transposed')
    pprint.pprint(transpose(table))
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481

    Simpler with numpy


    Code:
    >>> import numpy
    >>> numpy.loadtxt('a.txt')
    array([[ 1.,  2.],
           [ 3.,  4.]])
    >>> A = numpy.loadtxt('a.txt')
    >>> A.transpose()
    array([[ 1.,  3.],
           [ 2.,  4.]])
    >>> A.dot(A)
    array([[  7.,  10.],
           [ 15.,  22.]])
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by b49P23TIvg
    A solution (without matrix product)
    Code:
    import pprint
    
    textfile=raw_input('What is your textfile name? (Refrain from putting .txt):')
    name = textfile + '.txt'
    print 'loading',name
    
    with open(name,'r') as inf:
    	text_data = inf.readlines()
    
    table = []
    for Line in text_data:
        row = []
        for field in Line.strip().split():
            row.append(float(field))          # convert to float
        table.append(row)
    
    print('the table is:')
    pprint.pprint(table)
    
    def transpose(A):
        return list(zip(*A))
    
    print('transposed')
    pprint.pprint(transpose(table))
    Hi I looked through this code and I tried it out using PyLab. Everything worked up till I got the value error stating that I could not convert the string to float at row.append(float(field)). I tried the numpy way too but that also had the problem of converting string to float. Am I doing something wrong?
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481
    Yes, you did something wrong. Specifically you didn't share your input file. My test data:
    Code:
    1 2
    3 4
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0
    Ohhh I see. My text file was wrong the enter time! My file contained [[1,1],[2,3]]. But now that I think about it my program probably didn't need the matrix to be displayed as such in a text file. My apologies. Thanks for all your help though I understand it now!

IMN logo majestic logo threadwatch logo seochat tools logo