How to to read a matrix from a given file?

前端 未结 7 671
傲寒
傲寒 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:13

    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)]
    

提交回复
热议问题