开发者

How to to read a matrix from a given file?

I have a text file which contains matrix of N * M dimensions.

For example the input.txt file contains the following:

0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0
0,0,2,1,0,2,0,0,0,0
0,0,2,1,1,2,2,0,0,1
0,0,1,2,2,1,1,0,0,2
1,0,1,1,1,2,1,0,2,1

I开发者_Python百科 need to write python script where in I can import the matrix.

My current python script is:

f = open ( 'input.txt' , 'r')
l = []
l = [ line.split() for line in f]
print l

the output list comes like this

[['0,0,0,0,0,0,0,0,0,0'], ['0,0,0,0,0,0,0,0,0,0'], ['0,0,0,0,0,0,0,0,0,0'],
 ['0,0,0,0,0,0,0,0,0,0'], ['0,0,0,0,0,0,0,0,0,0'], ['0,0,0,0,0,0,0,0,0,0'],
 ['0,0,2,1,0,2,0,0,0,0'], ['0,0,2,1,1,2,2,0,0,1'], ['0,0,1,2,2,1,1,0,0,2'],
 ['1,0,1,1,1,2,1,0,2,1']]

I need to fetch the values in int form . If I try to type cast, it throws errors.


Consider

with open('input.txt', 'r') as f:
    l = [[int(num) for num in line.split(',')] for line in f]
print(l)

produces

[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 2, 1, 0, 2, 0, 0, 0, 0], [0, 0, 2, 1, 1, 2, 2, 0, 0, 1], [0, 0, 1, 2, 2, 1, 1, 0, 0, 2], [1, 0, 1, 1, 1, 2, 1, 0, 2, 1]]

Note that you have to split on commas.


If you do have blank lines then change

l = [[int(num) for num in line.split(',')] for line in f ]

to

l = [[int(num) for num in line.split(',')] for line in f if line.strip() != "" ]


You can simply use numpy.loadtxt. Easy to use, and you can also specify your delimiter, datatypes etc.

specifically, all you need to do is this:

import numpy as np
input = np.loadtxt("input.txt", dtype='i', delimiter=',')
print(input)

And the output would be:

[[0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 2 1 0 2 0 0 0 0]
 [0 0 2 1 1 2 2 0 0 1]
 [0 0 1 2 2 1 1 0 0 2]
 [1 0 1 1 1 2 1 0 2 1]]


You can do this:

fin = open('input.txt','r')
a=[]
for line in fin.readlines():
    a.append( [ int (x) for x in line.split(',') ] )


The following does what you want:

l = []
with open('input.txt', 'r') as f:
  for line in f:
    line = line.strip()
    if len(line) > 0:
      l.append(map(int, line.split(',')))
print l


You should not write your csv parser, consider the csv module when reading such files and use the with statement to close after reading:

import csv

with open('input.txt') ad f:
    data = [map(int, row) for row in csv.reader(f)]


Check out this small one line code for reading matrix,

matrix = [[input() for x in range(3)] for y in range(3)]

this code will read matrix of order 3*3.


import numpy as np
f = open ( 'input.txt' , 'r')
l = []
l = np.array([ line.split() for line in f])
print (l)
type(l)

output:

[['0']  ['0']  ['0']  ['0,0,0,0,0,0,0,0,0,0,0'] 
    ['0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0'] 
['0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0']] 

numpy.ndarray


The following code converts the above input to matrix form:

f = open ('input.txt' , 'r')
l = []
l = [line.split() for line in f]
l=np.array(l)
l=l.astype(np.int)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜