Read in list of lists of integers

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?

the issue is not with the matrix, the issue is that each element in list of lists is a string
this is one of them:

the quotes delimit the string

I can see that but I don’t know how to convert it to an integer.
All the examples I tried to follow did not work.

default separator is any white space

you are not splitting anything essentially

1 Like

I’m sorry but I do not understand.
The code I’ve copied has split() in it

list_of_lists = [(line.strip()).split() for line in a_file]

but it does not change it from a string to an integer.

I tried making it split(’) to split it on the inverted comma but that gave me an error.

you need to put a string as argument of split, you can read about he method in the link I posted above

I have tried
list_of_lists = [(line.strip()).split("’") for line in a_file]

and 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]]']]

which has the inverted commas around it.

I tried
list_of_lists = [(line.strip()).split(",") for line in a_file]

which gives me this garbage:

[[list(['[[5', '3', '0', '0', '7', '0', '0', '0', '0]', ''])
  list(['[6', '0', '0', '1', '9', '5', '0', '0', '0]', ''])
  list(['[0', '9', '8', '0', '0', '0', '0', '6', '0]', ''])
  list(['[8', '0', '0', '0', '6', '0', '0', '0', '3]', ''])
  list(['[4', '0', '0', '8', '0', '3', '0', '0', '1]', ''])
  list(['[7', '0', '0', '0', '2', '0', '0', '0', '6]', ''])
  list(['[0', '6', '0', '0', '0', '0', '2', '8', '0]', ''])
  list(['[0', '0', '0', '4', '1', '9', '0', '0', '5]', ''])
  list(['[0', '0', '0', '0', '8', '0', '0', '7', '9]]'])]]

what do you want to obtain?

I would say you are much near the result, if you remove the square brackets you get strings that contain only numbers, then you just need to convert the strings to integers

I took the [ ] off
list_of_lists = (line.strip()).split() for line in a_file
and now I get this error

File "main.py", line 16
    list_of_lists = (line.strip()).split() for line in a_file
                                           ^
SyntaxError: invalid syntax

I made it
list_of_lists = ((line.strip()).split() for line in a_file)

and got this error

[[<generator object <genexpr> at 0x7fa27a73c7b0>]]
------------------
Traceback (most recent call last):
  File "main.py", line 22, in <module>
    print(int(grid[0]))
TypeError: 'generator' object is not subscriptable

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

do you need to have the input file made like that?

It represents a sudoku and therefore it is easier to understand like that.

what if you write it without the square parenthesis?

I will look at that tomorrow.
Thanks for your help.

otherwise to remove the square parenthesis you can use something like replace, split on the commas, and then convert each element of the list to numbers using int, and so you get a list of lists of numbers

You can refer to regular expressions in python and separate the matching strings of the form: “[1,2,3,…]” from your sudoku file. After obtaining these lines as strings, you can split the string (ignoring the opening and closing brackets) using the “,” as the delimiter.
Then using chained functions as : list(map(int, string_var.split(","))) you can obtain a row of the sudoku.

for regular expressions you can refer this link:

I finally got it to do what I wanted.
https://repl.it/@rperrett/Sudoku-2#main.py

Somehow I think lines 27 to 46 could have been done much better.
But it does work.
Feel free to show me a better way if you like. This is a copy of my file.
Thanks for your suggestions. It was you suggesting I get rid of the [ ] that made me think that the file I was reading in could be a simple text file.
Thank you.