How to to read a matrix from a given file?

前端 未结 7 669
傲寒
傲寒 2020-12-09 17:41

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

For example the input.txt file contains the following:



        
7条回答
  •  执念已碎
    2020-12-09 18:18

    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

提交回复
热议问题