Home > Enterprise >  Why does this code not read the sudoku numbers from csv file and reconstruct it?
Why does this code not read the sudoku numbers from csv file and reconstruct it?

Time:07-02

This print_board is the code to reconstruct the the sudoku. it works perfectly fine when I use the sudoku numbers from the board, but it doesn't work from the sudoku.csv file. What have I done wrong?

""" Sudoku.csv = 
7;8;0;4;0;0;1;2;0
6;0;0;0;7;5;0;0;9
0;0;0;6;0;1;0;7;8
0;0;7;0;4;0;2;6;0
0;0;1;0;5;0;9;3;0
9;0;4;0;6;0;0;0;5
0;7;0;3;0;0;0;1;2
1;2;0;0;0;7;4;0;0
0;4;9;2;0;6;0;0;7 
"""

 #these are the sudoku numbers on the sudoku.csv file 


import csv
with open('sudoko.csv') as f:
    #next(f) 
    board = csv.reader(f,delimiter=';')
    for row in board:
        print(row)

#this is the code to read the sudoku.csv file.

def print_board(bo):
    for i in range(len(bo)):
        if i % 3 == 0 and i != 0:
            print("- - - - - - - - - - - - - ")

        for j in range(len(bo[0])):
            if j % 3 == 0 and j != 0:
                print(" | ", end="")

            if j == 8:
                print(bo[i][j])
            else:
                print(str(bo[i][j])   " ", end="")              
print(print_board(row))

CodePudding user response:

You can use this:

import csv


with open('sudoko.csv') as f:
    board = list(csv.reader(f,delimiter=';'))


def print_board():
    for row in range(len(board)):
        if row % 3 == 0:
            print('|', ('- '*3   '| ') * 3)
        for item in range(len(board[row])):
            if item % 3 == 0:
                print('| ', end='')
            print(board[row][item], end=' ')
        print('|')
    print('|', ('- '*3   '| ') * 3)


print_board()

Output:

| - - - | - - - | - - - | 
| 7 8 0 | 4 0 0 | 1 2 0 |
| 6 0 0 | 0 7 5 | 0 0 9 |
| 0 0 0 | 6 0 1 | 0 7 8 |
| - - - | - - - | - - - | 
| 0 0 7 | 0 4 0 | 2 6 0 |
| 0 0 1 | 0 5 0 | 9 3 0 |
| 9 0 4 | 0 6 0 | 0 0 5 |
| - - - | - - - | - - - | 
| 0 7 0 | 3 0 0 | 0 1 2 |
| 1 2 0 | 0 0 7 | 4 0 0 |
| 0 4 9 | 2 0 6 | 0 0 7 |
| - - - | - - - | - - - |

CodePudding user response:

There are multiple things that don't work.

  1. around the print_board function there should be no print function.
  2. don't pass a row to print_board, but the whole board
  3. rather use numpy to read csv. Then you get the board as an array

Here is a working example.

(Since I'm not sure how exactly your CSV looks like, you may have to change the delimiter argument)

import numpy as np

# change delimiter depending on your csv file
board = np.loadtxt('sudoko.csv', delimiter=";")

def print_board(bo):

    for i in range(len(bo)):
        if i % 3 == 0 and i != 0:
            print("- - - - - - - - - - - - - ")
        for j in range(len(bo[0])):
        
            if j % 3 == 0 and j != 0:
                print(" | ", end="")
            if j == 8:
                print(bo[i][j])
            else:
                print(str(bo[i][j])   " ", end="")


print_board(board)

So this is the output:

7.0 8.0 0.0  | 4.0 0.0 0.0  | 1.0 2.0 0.0
6.0 0.0 0.0  | 0.0 7.0 5.0  | 0.0 0.0 9.0
0.0 0.0 0.0  | 6.0 0.0 1.0  | 0.0 7.0 8.0
- - - - - - - - - - - - - 
0.0 0.0 7.0  | 0.0 4.0 0.0  | 2.0 6.0 0.0
0.0 0.0 1.0  | 0.0 5.0 0.0  | 9.0 3.0 0.0
9.0 0.0 4.0  | 0.0 6.0 0.0  | 0.0 0.0 5.0
- - - - - - - - - - - - - 
0.0 7.0 0.0  | 3.0 0.0 0.0  | 0.0 1.0 2.0
1.0 2.0 0.0  | 0.0 0.0 7.0  | 4.0 0.0 0.0
0.0 4.0 9.0  | 2.0 0.0 6.0  | 0.0 0.0 7.0
  • Related