Printing remaining clock time per move with the python-chess library

560 Views Asked by At

I want to sequentially print the remaining clock time (one string per move at a time) from a game I read (using the python-chess library) from a text file.

So, say I have a pgn file with a game that has the following moves...

  1. f3 { [%clk 0:05:00] } 1... e5 { [%clk 0:05:00] } 2. g4 { [%clk 0:04:49] } 2... Qh4# { [%clk 0:04:48] }

... I would like to iterate through the moves and print the corresponding clock time one by one (using a for loop or similar), showing

0:05:00

0:05:00

0:04:49

0:04:48

I know how to iterate through the moves of a game from: Printing individual moves with the python-chess library

I changed the code from that answer to


pgn = open("test.pgn")
game = chess.pgn.read_game(pgn)

board = game.board()

for move in game.mainline_moves():
    print(game.variation(move))
    board.push(move)

But that did not work. In the first iteration it did print the complete variation (all moves and clocktime all in one line). In the second iteration it raised an error:

"KeyError: Move.from_uci('e7e5')"

1

There are 1 best solutions below

0
On

Hints: