I have a list of lists (integers) representing a sudoku. I copied code from:
https://www.youtube.com/watch?v=G_UYXzGuqvM
I’m trying to use a .txt file for the sudoku data so I can read in different sudokus.
I have a file called sudoku01.txt which has
[[5,3,0,0,7,0,0,0,0],
[6,0,0,1,9,5,0,0,0],
[0,9,8,0,0,0,0,6,0],
[8,0,0,0,6,0,0,0,3],
[4,0,0,8,0,3,0,0,1],
[7,0,0,0,2,0,0,0,6],
[0,6,0,0,0,0,2,8,0],
[0,0,0,4,1,9,0,0,5],
[0,0,0,0,8,0,0,7,9]]
The code I have found and have tried is
import numpy as np
a_file = open("sudoku01.txt", "r")
list_of_lists = [(line.strip()).split() for line in a_file]
a_file.close()
print(list_of_lists)
print()
print(np.matrix(list_of_lists))
However, it gives me
[['[[5,3,0,0,7,0,0,0,0],'], ['[6,0,0,1,9,5,0,0,0],'], ['[0,9,8,0,0,0,0,6,0],'], ['[8,0,0,0,6,0,0,0,3],'], ['[4,0,0,8,0,3,0,0,1],'], ['[7,0,0,0,2,0,0,0,6],'], ['[0,6,0,0,0,0,2,8,0],'], ['[0,0,0,4,1,9,0,0,5],'], ['[0,0,0,0,8,0,0,7,9]]']]
[['[[5,3,0,0,7,0,0,0,0],']
['[6,0,0,1,9,5,0,0,0],']
['[0,9,8,0,0,0,0,6,0],']
['[8,0,0,0,6,0,0,0,3],']
['[4,0,0,8,0,3,0,0,1],']
['[7,0,0,0,2,0,0,0,6],']
['[0,6,0,0,0,0,2,8,0],']
['[0,0,0,4,1,9,0,0,5],']
['[0,0,0,0,8,0,0,7,9]]']]
Note all the single inverted commas.
I have looked at lots of forums giving answers to other people but I am not getting anywhere. I need the data to be assigned to a variable similar to this:
grid = [[5,3,0,0,7,0,0,0,0],
[6,0,0,1,9,5,0,0,0],
[0,9,8,0,0,0,0,6,0],
[8,0,0,0,6,0,0,0,3],
[4,0,0,8,0,3,0,0,1],
[7,0,0,0,2,0,0,0,6],
[0,6,0,0,0,0,2,8,0],
[0,0,0,4,1,9,0,0,5],
[0,0,0,0,8,0,0,7,9]]
How can I get rid of the unwanted inverted commas?