Answers.Body: <p>I used the following python script using <a href="https://python-chess.readthedocs.io/en/latest/#" rel="nofollow noreferrer"><code>python-chess</code> library</a> :</p>
  
  <pre><code>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 &lt; nb_games:
          game = chess.pgn.read_game(pgn_file)
          board = game.board()
          has_wanted_position = False
          wanted_fen = ""
          for move in game.mainline_moves():
              board.push(move)
              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 &lt; 2)
              #cond_symmetrical = (nb_b == 2 and nb_B &lt; 2)
  
              if ((cond or cond_symmetrical) and no_pieces):
                  has_wanted_position = True
                  wanted_fen = board.fen()
                  break
              else:
                  continue
          #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("\n")
              output_pgn.write("[FEN \"" + wanted_fen + "\"]")
              output_pgn.write("\n")
              output_pgn.write("\n")
          games += 1
      output_pgn.close()
      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")
  </code></pre>
  
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-18T10:53:35.760Z
Answers.LastActivityDate: 2020-02-18T18:56:09.260Z
Answers.LastEditDate: 2020-02-18T18:56:09.260Z
Answers.UserDisplayName: Olórin
Answers.Body: <p>To be honest, I am not 100% sure what you are asking for by "up to white/black symmetry."</p>
  
  <p>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).</p>
  
  <p>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.</p>
  
  <p><a href="https://i.sstatic.net/ARw3O.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ARw3O.png" alt="enter image description here"></a></p>
  
  <p>Next under File>Options>Misc, check the "theme keys box".</p>
  
  <p><a href="https://i.sstatic.net/Xkb33.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Xkb33.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.sstatic.net/2SFnN.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/2SFnN.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.sstatic.net/X6DHq.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/X6DHq.png" alt="enter image description here"></a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-17T21:18:11.927Z
Answers.Text: By "KPvK up to colour symmetry" I mean "KPvK or KvKP". Let me see what I get when I enable the themes
Answers.UserDisplayName: Olórin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-17T21:23:19.523Z
Answers.Text: 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".
Answers.UserDisplayName: Olórin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-17T21:26:02.257Z
Answers.Text: @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.
Answers.UserDisplayName: PhishMaster
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-17T21:36:27.780Z
Answers.Text: 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.
Answers.UserDisplayName: Olórin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-17T21:42:57.317Z
Answers.Text: 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.
Answers.UserDisplayName: Olórin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-17T21:45:26.807Z
Answers.Text: To be precise : using you "ending tab" filtering method or the one I describe in my question, I end with a list of games, and if I click on any game in the list, the board will open the game at the first position where there only two kings and a pawn. The gives a position P(G) per game G. I would like to export the list of P(G)'s for all game G in the list, in a PGN file.
Answers.UserDisplayName: Olórin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-17T22:17:14.057Z
Answers.Text: What I am trying to do : in a given chessbase database (let's say the metadatabase 2020), find the list L of all games having let's say KPvK (up to symmetry) ending (apparently this already not possible), for each such game G in the list L find the first position P(G) of the game where there is only two kings and a pawn. When G varies in L, the P(G)'s give a list M of positions (one position per game in L). I want to export the list M in a pgn file. I hope I make more sense now ?
Answers.UserDisplayName: Olórin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-17T22:28:57.813Z
Answers.Text: First, the part about symmetry, I finally got it, and you mean "up to equal material". It is not going to be possible for it to truncate the the games, and only show the position from the K+P ending. That would be a manual task.
Answers.UserDisplayName: PhishMaster
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-18T07:58:28.357Z
Answers.Text: Ah ah now I don't understand you. So I repeat : by "KPvK" I mean "white king + one pawn vs black king" and by "KPvK up to colour symmetry" I mean "white king + one pawn vs black king or black king + one pawn vs white king". As for the truncation, I think that I will select the whole games list obtained after filtering, right click on "output" and choose text file, PGN format. Then I will have to parse this file and for each game in it convert the game in a list of FEN positions (one per game move), and then parse this FEN list get the first FEN matching the material (KPvK or KvKP) I want.
Answers.UserDisplayName: Olórin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-18T07:58:45.463Z
Answers.Text: And I will have to code to do it. :)
Answers.UserDisplayName: Olórin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-18T08:52:48.963Z
Answers.Text: Actually it seems that I won't have to code much for it, as there is a python chess library https://python-chess.readthedocs.io/en/latest/index.html allowing nice manipulation of PGN's. Will post the final code in the answer I will give to my own question.
Answers.UserDisplayName: Olórin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-18T10:54:03.220Z
Answers.Text: Final code posted as an answer
Answers.UserDisplayName: Olórin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-17T20:56:59.650Z
Answers.LastActivityDate: 2020-02-17T20:56:59.650Z
Answers.UserDisplayName: PhishMaster
Body: <p>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.</p>
  
  <p>For this I open the Mega Database in ChessBase 15 and I do "Filter List --> Material". The best I can achieve so far is :</p>
  
  <p><a href="https://i.sstatic.net/w5Nr0.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/w5Nr0.png" alt="enter image description here"></a></p>
  
  <p>(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 <a href="https://en.chessbase.com/support-kb/content/details/1053/Material_searches_with_ChessBase" rel="nofollow noreferrer">https://en.chessbase.com/support-kb/content/details/1053/Material_searches_with_ChessBase</a> but it did not help very much, but that is another question ...)</p>
  
  <p>This gives a list of games, and if I click on the line corresponding to a game, the position appears for instance as follows :</p>
  
  <p><a href="https://i.sstatic.net/dsqWQ.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/dsqWQ.jpg" alt="enter image description here"></a></p>
  
  <p>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.</p>
  
  <p><strong>Is it possible to export all of these positions in a database, retaining the ply move plus players, dates and others usual information?</strong></p>
  
  <p>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.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-02-17T19:20:39.730Z
Comments.Text: Dont have chessbase but I guess you click on pieces   [rather than no pieces]     and then select just kings for W/B.
Comments.UserDisplayName: edwina oliver
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-02-18T09:21:40.497Z
Comments.Text: @Olorin Sounds like something that you can easily solve by yourself with a bit of scripting. I recommend using [python-chess](https://python-chess.readthedocs.io/en/latest/) 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](https://chess.stackexchange.com/a/24177/3594). By analogy, first figure out the shape of FENs that match your requirement then look for that pattern.
Comments.UserDisplayName: Ellie
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-02-18T09:53:44.073Z
Comments.Text: @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.
Comments.UserDisplayName: Olórin
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-02-18T09:56:39.593Z
Comments.Text: @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
Comments.UserDisplayName: Olórin
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-02-18T09:57:54.693Z
Comments.Text: @edwinaoliver This did not work.
Comments.UserDisplayName: Olórin
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-02-18T10:54:17.937Z
Comments.Text: @Phonon Final code posted as an answer
Comments.UserDisplayName: Olórin
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-02-18T11:00:44.773Z
Comments.Text: @Olorin Great to see you opted for this solution, well done! Might want to update the question post/title accordingly for future readers.
Comments.UserDisplayName: Ellie
Tags: <software><chessbase><python>
Title: Export KPvK endings positions (up to black/white symmetry) from ChessBase 15 using python-chess
ThreadText: # Export KPvK endings positions (up to black/white symmetry) from ChessBase 15 using python-chess
  
  (Asked by: username_0 on 2020-02-17)
  
  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](https://i.sstatic.net/w5Nr0.png)](https://i.sstatic.net/w5Nr0.png)
  
  (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](https://i.sstatic.net/dsqWQ.jpg)](https://i.sstatic.net/dsqWQ.jpg)
  
  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.
  
  **Tags:** software, chessbase, python
  
  > **Comments:**
  > **username_1**: Dont have chessbase but I guess you click on pieces   [rather than no pieces]     and then select just kings for W/B.  
  > **username_2**: @Olorin Sounds like something that you can easily solve by yourself with a bit of scripting. I recommend using [python-chess](https://python-chess.readthedocs.io/en/latest/) 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](https://chess.stackexchange.com/a/24177/3594). By analogy, first figure out the shape of FENs that match your requirement then look for that pattern.  
  > **username_0**: @Phonon Yes, this is actually the conclusion I drew, see my comment to username_3'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.  
  > **username_0**: @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  
  > **username_0**: @username_1 This did not work.  
  > **username_0**: @Phonon Final code posted as an answer  
  > **username_2**: @Olorin Great to see you opted for this solution, well done! Might want to update the question post/title accordingly for future readers.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2020-02-18. Score: 3
  
  I used the following python script using [`python-chess` library](https://python-chess.readthedocs.io/en/latest/#) :
  
  ```
  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():
              board.push(move)
              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()
                  break
              else:
                  continue
          #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("\n")
              output_pgn.write("[FEN \"" + wanted_fen + "\"]")
              output_pgn.write("\n")
              output_pgn.write("\n")
          games += 1
      output_pgn.close()
      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.
  
  
  ---
  
  ### Answer by username_3 on 2020-02-17. Score: 0
  
  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](https://i.sstatic.net/ARw3O.png)](https://i.sstatic.net/ARw3O.png)
  
  Next under File>Options>Misc, check the "theme keys box".
  
  [![enter image description here](https://i.sstatic.net/Xkb33.png)](https://i.sstatic.net/Xkb33.png)
  
  [![enter image description here](https://i.sstatic.net/2SFnN.png)](https://i.sstatic.net/2SFnN.png)
  
  [![enter image description here](https://i.sstatic.net/X6DHq.png)](https://i.sstatic.net/X6DHq.png)
  
  > **Comments:**
  > **username_0**: By "KPvK up to colour symmetry" I mean "KPvK or KvKP". Let me see what I get when I enable the themes  
  > **username_0**: 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".  
  > **username_3**: @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.  
  > **username_0**: 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.  
  > **username_0**: 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.  
  > **username_0**: To be precise : using you "ending tab" filtering method or the one I describe in my question, I end with a list of games, and if I click on any game in the list, the board will open the game at the first position where there only two kings and a pawn. The gives a position P(G) per game G. I would like to export the list of P(G)'s for all game G in the list, in a PGN file.  
  > **username_0**: What I am trying to do : in a given chessbase database (let's say the metadatabase 2020), find the list L of all games having let's say KPvK (up to symmetry) ending (apparently this already not possible), for each such game G in the list L find the first position P(G) of the game where there is only two kings and a pawn. When G varies in L, the P(G)'s give a list M of positions (one position per game in L). I want to export the list M in a pgn file. I hope I make more sense now ?  
  > **username_3**: First, the part about symmetry, I finally got it, and you mean "up to equal material". It is not going to be possible for it to truncate the the games, and only show the position from the K+P ending. That would be a manual task.  
  > **username_0**: Ah ah now I don't understand you. So I repeat : by "KPvK" I mean "white king + one pawn vs black king" and by "KPvK up to colour symmetry" I mean "white king + one pawn vs black king or black king + one pawn vs white king". As for the truncation, I think that I will select the whole games list obtained after filtering, right click on "output" and choose text file, PGN format. Then I will have to parse this file and for each game in it convert the game in a list of FEN positions (one per game move), and then parse this FEN list get the first FEN matching the material (KPvK or KvKP) I want.  
  > **username_0**: And I will have to code to do it. :)  
  > **username_0**: Actually it seems that I won't have to code much for it, as there is a python chess library https://python-chess.readthedocs.io/en/latest/index.html allowing nice manipulation of PGN's. Will post the final code in the answer I will give to my own question.  
  > **username_0**: Final code posted as an answer  
  
  ---
  
  