Skip to content

Latest commit

 

History

History
131 lines (86 loc) · 3.04 KB

pgn.rst

File metadata and controls

131 lines (86 loc) · 3.04 KB

PGN parsing and writing

Game model

Games are represented as a tree of moves. Each GameNode can have extra information such as comments. The root node of a game (Game extends GameNode) also holds general information, such as game headers.

chess.pgn.Game

chess.pgn.GameNode

Parsing

chess.pgn.read_game

chess.pgn.scan_headers

chess.pgn.scan_offsets

Writing

If you want to export your game game with all headers, comments and variations you can use:

>>> print(game) [Event "?"] [Site "?"] [Date "????.??.??"] [Round "?"] [White "?"] [Black "?"] [Result ""] <BLANKLINE> 1. e4 e5 { Comment }

Remember that games in files should be separated with extra blank lines.

>>> print(game, file=handle, end="nn")

Use exporter objects if you need more control. Exporter objects are used to allow extensible formatting of PGN like data.

chess.pgn.StringExporter

chess.pgn.FileExporter

NAGs

Numeric anotation glyphs describe moves and positions using standardized codes that are understood by many chess programs. During PGN parsing, annotations like !, ?, !!, etc. are also converted to NAGs.