lichess.org
Donate

How to extract the CLK tag of PGN

Hi,
I would like to know how I can extract the clock information ( time remaining ) from PGN file using a program.
Python is preferable. I have stumbled on a easy python program python-chess.readthedocs.io/en/latest/pgn.html. I am struggling with how to extract clock information from PGN file. Thanks in advance.
1. d4 { [%clk 0:02:00] } 1... d5 { [%clk 0:02:00] } , I am able to extract the moves using "for move in first_game.mainline_moves():"
and this spits out the entire game as string - first_game.mainline(). I would like to know how to systematically extract the move-no, clock information.
I'm not really familiar with how pythonchess deals with pgn files, but if this feature doesn't exist in the library, maybe you can read the pgn as if it were a .txt file and extract the file manually (find all the occurences of "%clk" and the following text will be the time. Stop when you reach a "]").
If you turn to the source code, a game node is defined like this:

github.com/niklasf/python-chess/blob/e9a975fcec2fbf5f929f8f40b58458b1c7c99da1/chess/pgn.py#L123

Pgn comment of a node can accessed as node.comment.

If instead of mainline_moves, you iterate mainline:

github.com/niklasf/python-chess/blob/e9a975fcec2fbf5f929f8f40b58458b1c7c99da1/chess/pgn.py#L314

then you get an interator for the nodes, rather than moves and you get the comments using something like:

for node in first_game.mainline():
... print(node.comment)

Once you have the comment, you have to cook up a regular expression to extract clock information from the comment.
#8

Yes, as far as I know the comment can have new lines in it. I know for certain that Python chess exports comments containing new lines because I used it to store multi line comments and they parsed. Also I know that Python chess rigorously adheres to the pgn standard, so it must be the case that the pgn standard allows this.
Ok, Then he would probably first do comment.replace('\n', '') and then apply your regex. Depends on what he wants to do exactly.

This topic has been archived and can no longer be replied to.