Reading from a file

from sgfmill import sgf
with open("foo.sgf", "rb") as f:
    game = sgf.Sgf_game.from_bytes(f.read())
winner = game.get_winner()
board_size = game.get_size()
root_node = game.get_root()
b_player = root_node.get("PB")
w_player = root_node.get("PW")
for node in game.get_main_sequence():

Recording a game

from sgfmill import sgf
game = sgf.Sgf_game(size=13)
for move_info in ...:
    node = game.extend_main_sequence()
    node.set_move(move_info.colour, move_info.move)
    if move_info.comment is not None:
        node.set("C", move_info.comment)
with open(pathname, "wb") as f:

Modifying a game

>>> from sgfmill import sgf
>>> game = sgf.Sgf_game.from_string("(;FF[4]GM[1]SZ[9];B[ee];W[ge])")
>>> root_node = game.get_root()
>>> root_node.set("RE", "B+R")
>>> new_node = game.extend_main_sequence()
>>> new_node.set_move("b", (2, 3))
>>> [node.get_move() for node in game.get_main_sequence()]
[(None, None), ('b', (4, 4)), ('w', (4, 6)), ('b', (2, 3))]
>>> game.serialise()

See also the show_sgf.py and split_sgf_collection.py example scripts.

The example scripts

The following example scripts are available in the examples/ directory of the Sgfmill source distribution.

They may be independently useful, as well as illustrating the library API.

See the top of each script for further information.

See running the example scripts for notes on making the sgfmill package available for use with the example scripts.


Prints an ASCII diagram of the position from an SGF file.

This demonstrates the sgf, sgf_moves, and ascii_boards modules.


Splits a file containing an SGF game collection into multiple files.

This demonstrates the parsing functions from the sgf_grammar module.