The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Reading text file containing square matrix
Discuss Reading text file containing square matrix in the Python Programming forum on Dev Shed. Reading text file containing square matrix Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 3rd, 2012, 05:37 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 3
Time spent in forums: 27 m 43 sec
Reputation 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!
|

December 3rd, 2012, 06:39 PM
|
 |
Contributing User
|
|
|
|
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!
|

December 3rd, 2012, 06:44 PM
|
 |
Contributing User
|
|
|
|
|
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.]])
>>>
|

December 4th, 2012, 12:41 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 3
Time spent in forums: 27 m 43 sec
Reputation Power: 0
|
|
Quote: | 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?
|

December 4th, 2012, 07:42 AM
|
 |
Contributing User
|
|
|
|
Yes, you did something wrong. Specifically you didn't share your input file. My test data:
|

December 4th, 2012, 01:42 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 3
Time spent in forums: 27 m 43 sec
Reputation 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!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|