I have the Mega Database 2020 from ChessBase and ChessBase 15. I am using ChessBase 15 to search the Mega Database 2020, my search criterion being all games ended through a KPvK (king and pawn vs. king) up to white/black symmetry.

For this I open the Mega Database in ChessBase 15 and I do "Filter List --> Material". The best I can achieve so far is :

enter image description here

(If I click on "No pieces" as I should, it resets the "Total" four cases, and I don't understand why, so that I am not sure that my filter does exactly want I and gives me all games I really want. I checked on https://en.chessbase.com/support-kb/content/details/1053/Material_searches_with_ChessBase but it did not help very much, but that is another question ...)

This gives a list of games, and if I click on the line corresponding to a game, the position appears for instance as follows :

enter image description here

We see that the board is automatically set up to the position of game defined by the ply where the KPvK (up to colour symmetry) begins.

Is it possible to export all of these positions in a database, retaining the ply move plus players, dates and others usual information?

This could allow to create a KPvK training database. Of course, it is somewhat trivial regarding the nature of the ending, but the principle would be useful for other types of endings.

  • 1
    Dont have chessbase but I guess you click on pieces [rather than no pieces] and then select just kings for W/B. Commented Feb 17, 2020 at 19:20
  • @Olorin Sounds like something that you can easily solve by yourself with a bit of scripting. I recommend using python-chess for such purposes, and you'll also find various solved examples here on chess SE using that library. For instance here's one on parsing PGNs in order to find games with quadrupled pawn structure. By analogy, first figure out the shape of FENs that match your requirement then look for that pattern.
    – Ellie
    Commented Feb 18, 2020 at 9:21
  • @Phonon Yes, this is actually the conclusion I drew, see my comment to PhishMaster's answer. I already wrote a python script and I am actually just debugging it. Will post it as an answer when it will be done.
    – Olórin
    Commented Feb 18, 2020 at 9:53
  • @Phonon Thank's for the link, it will be useful later for sure. In my case it is quite easy, I loop on games in the pgn, then on moves of the games by pushing them on the board, to each such board I get the board FEN (without castling info) and in this FEN I just search get the occurences of the pieces/pawns and check it they match the occurences of the pieces/pawns I want in my ending and then I print that in a new pgn, without games algebraic notations but with a FEN header, the FEN being the FEN of the position I want if it exists in the game
    – Olórin
    Commented Feb 18, 2020 at 9:56
  • 1
    @edwinaoliver This did not work.
    – Olórin
    Commented Feb 18, 2020 at 9:57

2 Answers 2


I used the following python script using python-chess library :

import chess
import chess.pgn

def KPvK_up_to_symmetry_export_to_FEN_func(pgn_file_path, nb_games, output_pgn_file_name):
    output_pgn = open(output_pgn_file_name,"w+")
    games = 0
    pgn_file = open(pgn_file_path, encoding="utf-8-sig")
    while nb_games == 0 or games < nb_games:
        game = chess.pgn.read_game(pgn_file)
        board = game.board()
        has_wanted_position = False
        wanted_fen = ""
        for move in game.mainline_moves():
            fen = board.board_fen()

            nb_p = fen.count("p")
            nb_P = fen.count("P")

            nb_n = fen.count("n")
            nb_N = fen.count("N")

            nb_b = fen.count("b")
            nb_B = fen.count("B")

            nb_r = fen.count("r")
            nb_R = fen.count("R")

            nb_q = fen.count("q")
            nb_Q = fen.count("Q")

            nb_k = fen.count("k")
            nb_K = fen.count("K")

            no_pieces = ((nb_n == 0) and (nb_N == 0) and (nb_b == 0) and (nb_B == 0) and (nb_r == 0) and (nb_R == 0) and (nb_q == 0) and (nb_Q == 0))

            cond = ((nb_k == 1) and (nb_K == 1) and (nb_P == 1) and (nb_p == 0)) #KPvK
            cond_symmetrical = ((nb_k == 1) and (nb_K == 1) and (nb_P == 0) and (nb_p == 1)) #KvKP

            #cond = (nb_B == 2 and nb_b < 2)
            #cond_symmetrical = (nb_b == 2 and nb_B < 2)

            if ((cond or cond_symmetrical) and no_pieces):
                has_wanted_position = True
                wanted_fen = board.fen()
        #We are out of the loop on game moves, with a FEN or with nothing (if the game hasn't the ending we want)
        if (has_wanted_position):
            for header_key, header_value in game.headers.items():
                output_pgn.write("[" + header_key + " \"" + header_value + "\"]")
            output_pgn.write("[FEN \"" + wanted_fen + "\"]")
        games += 1
    return None

nb_games_total = 1523849
KPvK_up_to_symmetry_export_to_FEN_func(r"C:\CHESS\PYTHON\megadatabase2020.pgn", nb_games_total, "megadatabase2020_KPvP.pgn")

I did this for the pgn file I got from chessbase by exporting to text PGN my whole megadatabase 2020. Executing this script will produce a pgn file containing positions, that I'll be able to use back in chessbase.


To be honest, I am not 100% sure what you are asking for by "up to white/black symmetry."

I think this solution will help you find what you are looking for. Theme keys are not enabled by default in ChessBase. There are amazingly detailed Theme, Tactics, Strategy, and Endgame keys built into Mega 2020 (and prior versions).

Take a look at the difference in the tabs with the theme keys enables. Once you have them enabled, you can take whatever games you want, and put them in the clipboard, and export the clipboard to a new database.

enter image description here

Next under File>Options>Misc, check the "theme keys box".

enter image description here

enter image description here

enter image description here

  • By "KPvK up to colour symmetry" I mean "KPvK or KvKP". Let me see what I get when I enable the themes
    – Olórin
    Commented Feb 17, 2020 at 21:18
  • Ok, when I check the "Use Theme Key box, and when I open the database and go to the endings tab, I have an empty windows with four buttons : "select key", "install empty key", "intall big key", "install small key".
    – Olórin
    Commented Feb 17, 2020 at 21:23
  • @Olorin, did you pay for the database, and install it from the DVD? Or did you just get the database files? If you did not install it from the DVD, I could see them not being there. Commented Feb 17, 2020 at 21:26
  • 1
    I paid the web version of it and installed it after download. I just clicked the "install big key" button and after some time it installed me everything and I have the same as you. Let me see in details now.
    – Olórin
    Commented Feb 17, 2020 at 21:36
  • Ok, I am in the pawn endings, in the "1..8 P : K" section. The name of the section makes me thing that it means "K + 1 up to 8 P against K" but so far I see only "KPvK up to symmetry" games in it. Now I will see if I can export the desired positions, because that is the point of my question.
    – Olórin
    Commented Feb 17, 2020 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.