Python - Read list within matrix, which has been saved in text file

0

Greetings.

I have a 16 x 8 matrix that, in turn, contains lists in some of its coordinates. I know how to send the matrix to a text file, but I can not take the data from the file and rewrite it in a new matrix as integers, respecting the internal lists.

This is an example of the matrix

That's the matrix in the text file. Now I want to return it to an array in Python. I have seen that some use numpy, but since I am using python 3.5.2, I do not load the library. I would like to know if you can give me another method to achieve my goal. In advance, thank you very much to anyone who can help me.

    
asked by Cesar Alvarez 26.02.2017 в 02:06
source

1 answer

2

You can do assi, using json :

import json
matx = [1,2,3,4,5, [1,2,3, [1,2]]]

To send the matrix to a text file:

with open('tests.txt', 'w') as f:
    json.dump(matx, f)

To take the data from the file:

with open('tests.txt', 'r') as f:
    matx = json.load(f)

print(matx) # [1, 2, 3, 4, 5, [1, 2, 3, [1, 2]]]
    
answered by 26.02.2017 / 10:49
source