Skip to content
Advertisement

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:

JavaScript

I need to write python script where in I can import the matrix.

My current python script is:

JavaScript

the output list comes like this

JavaScript

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

Advertisement

Answer

Consider

JavaScript

produces

JavaScript

Note that you have to split on commas.


If you do have blank lines then change

JavaScript

to

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement