Im working wiht a list of lists that ended up containing color codes surrounding integer elements in the sublists. This list of lists is meant to be printed into the console as a sudoku grid with gray cells by using a function that replaces the element already present, according to a coordinates list, by the element surrounded by the color codes. However, it would be ideal if i could not save the color codes into the file by removing them priorly or not having them saved into a variable.
This is the function used to save into a file:
def board_saver(board,str_filename):
filename = open(str_filename,'w')
for i in board:
if board.index(i) == 0:
filename.write("[%s,\n" % i)
else:
filename.write("%s,\n" % i if board.index(i) in [1,2,3,4,5,6,7] else "%s]\n" % i)
filename.close()
And this is the function used to read the same list of lists into python again, including, allowing further filling of the list of lists through the console:
def board_reader(str_filename):
file1 = open(str_filename, 'r')
board2 = eval(file1.read())
print(type(board2))
file1.close()
return board2
Here is what i have been saving into a file:
[[5, 7, ' ', 8, 2, ' ', 4, ' ', ' '],
[3, '\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m \x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m', '\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m \x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m', '\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m9\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m', '\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m \x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m', 6, ' ', 5, ' '],
[' ', '\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m \x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m', 1, ' ', ' ', ' ', ' ', 3, 7],
[2, '\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m \x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m', '\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m \x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m', '\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m \x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m', '\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m7\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m', ' ', 8, 1, ' '],
[' ', 1, '\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m \x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m', ' ', 4, ' ', '\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m \x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m', 6, ' '],
[7, 4, ' ', ' ', ' ', ' ', '\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m8\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m', ' ', ' '],
[' ', ' ', 5, ' ', ' ', ' ', '\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m5\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m', '\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m9\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m', '\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m\x1b[1;37;40m \x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m']]
The functions that produce the list of lists above are:
print("gray cells:",coordinates_list,)
sudoku2 = board
for i in coordinates_list:
element = i
x = int(element[-1])
y = int(element[0])
if element == "{}{}".format(y,x) :
sudoku2[y][x] = "\033[1;37;40m{}\033[0m".format(board[y][x])
return sudoku2
def sudoku_printer(coordinates_list, board):
""" Prints a board line by line with the elements from sudoku_board being placced in the
{} place holders by the comprehention list in the format function. Because of board_fuser
indexes qith zero are printed with a grey color"""
board1 = board
fused_board = board_fusor(coordinates_list, board1)
print("fused board", board1)
board_header2 = " 1 2 3 4 5 6 7 8 9"
row_names = ["A", "B", "C", "D", "E", "F", "G", "H", "I"]
print("\n", board_header2)
print("-"*37)
for i, row in enumerate(fused_board):
print(("|" + " {} {} {} |"*3).format(*[x if x != 0 else "{}".format(x) for x in row]),row_names[i])
if i == 8:
print("-"*37)
elif i % 3 == 2:
print("|" + "---+"*8 + "---|")
else:
print("|" + " +"*8 + " |")
When it should be/look like this:
[[5, 7, ' ', 8, 3, ' ', 4, ' ', ' '],
[3, ' ', ' ', 9, ' ', ' ', ' ', 5, ' '],
[' ', ' ', 1, ' ', ' ', ' ', ' ', 3, 7],
[' ', ' ', ' ', ' ', 7, ' ', 8, 1, ' '],
[2, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[1, ' ', ' ', ' ', 8, ' ', ' ', ' ', 6],
[' ', 1, ' ', ' ', 4, ' ', ' ', 6, ' '],
[7, 4, ' ', ' ', ' ', ' ', 8, ' ', ' '],
[' ', ' ', 5, ' ', ' ', ' ', 5, 9, ' ']]
Can anyone help me please? What should I do to remove the color code part from the elements of the list of lists or to remove it after reading it from the file? Is there an answer like: remove color codes from console text output to do that? I do not understand RegEx.