Skip to content

Convert your pgn db to JSON (works with variations, each move has FEN code of position). Group (merge) games in PGN db by player name (white or black) or your own rules.

Notifications You must be signed in to change notification settings

Allirey/PGN-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

My tools to process chess files (PGN)

  • pgn_to_json_light.py - convert pgn db to json, can handle variations, nag tags, comments, no dependency.
  • pgn_to_json.py - convert pgn db to json, can handle variations, nag tags, comments, moves includes fen code of position. Dependency: python-chess
  • pgn_merge.py - for group (merge) pgn db by player name (white or black), or your own rules

IMPORTANT:

there is no PGN validation check! Parser expect that your PGN is already valid.

Usage for pgn_to_json_light:

usage: pgn_to_json_light.py [-h] [-nc] pathname

Convert pgn to json

positional arguments:
  pathname    path to your pgn file

optional arguments:
  -h, --help  show this help message and exit
  -ic         ignore comments. Converts ignoring text commentaries

example:

your_pgn_db.pgn
[Event "F/S Return Match"]
[Site "Belgrade, Serbia JUG"]
[Date "1992.11.04"]
[Round "29"]
[White "Fischer, Robert J."]
[Black "Spassky, Boris V."]
[Result "1/2-1/2"]

1. e4 (1. d4 $1 {Best move in this position }) e5 2. Nf3 Nc6 3. Bb5 a6 {This opening is called the Ruy Lopez.}
4. Ba4 Nf6 5. O-O Be7 6. Re1 b5 7. Bb3 d6 8. c3 O-O 9. h3 Nb8 10. d4 Nbd7
11. c4 c6 12. cxb5 axb5 13. Nc3 Bb7 14. Bg5 b4 15. Nb1 h6 16. Bh4 c5 17. dxe5
Nxe4 18. Bxe7 Qxe7 19. exd6 Qf6 20. Nbd2 Nxd6 21. Nc4 Nxc4 22. Bxc4 Nb6
23. Ne5 Rae8 24. Bxf7+ Rxf7 25. Nxf7 Rxe1+ 26. Qxe1 Kxf7 27. Qe3 Qg5 28. Qxg5
hxg5 29. b3 Ke6 30. a3 Kd6 31. axb4 cxb4 32. Ra5 Nd5 33. f3 Bc8 34. Kf2 Bf5
35. Ra7 g6 36. Ra6+ Kc5 37. Ke1 Nf4 38. g3 Nxh3 39. Kd2 Kb5 40. Rd6 Kc5 41. Ra6
Nf2 42. g4 Bd3 43. Re6 1/2-1/2
run command:
python3 pgn_to_json_light.py your_pgn_db.pgn

result:

# your_pgn_db.pgn.json:

[
  {
    "headers": {
      "Event": "F/S Return Match",
      "Site": "Belgrade, Serbia JUG",
      "Date": "1992.11.04",
      "Round": "29",
      "White": "Fischer, Robert J.",
      "Black": "Spassky, Boris V.",
      "Result": "1/2-1/2"
    },
    "moves": [
      {
        "san": "e4",
        "variations": [
          [
            {
              "san": "d4",
              "nag": [
                "$1"
              ],
              "comment": "Best move in this position"
            }
          ]
        ]
      },
      {
        "san": "e5"
      },
      {
        "san": "Nf3"
      },
      {
        "san": "Nc6"
      },
      {
        "san": "Bb5"
      },
      {
        "san": "a6",
        "comment": "This opening is called the Ruy Lopez."
      },
      
      . . .
      
      (282 lines)

Usage for pgn_to_json:

currently there is no convenient way to run script, you need to manually edit pgn_to_json.py for changing path to your PGN file:

if __name__ == '__main__':
    path_to_file = 'path_to_your.pgn'  # <- specify path to PGN here.
    comments = True  # change to False if you need ignore comments

or you can include pgn_to_json.py file in your python program and import pgn_to_json function

example result for pgn_to_json.py:

[
  {
    "headers": {
      "Event": "F/S Return Match",
      "Site": "Belgrade, Serbia JUG",
      "Date": "1992.11.04",
      "Round": "29",
      "White": "Fischer, Robert J.",
      "Black": "Spassky, Boris V.",
      "Result": "1/2-1/2"
    },
    "moves": [
      {
        "san": "e4",
        "from": "e2",
        "to": "e4",
        "fen": "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1",
        "variations": [
          [
            {
              "san": "d4",
              "from": "d2",
              "to": "d4",
              "fen": "rnbqkbnr/pppppppp/8/8/3P4/8/PPP1PPPP/RNBQKBNR b KQkq - 0 1",
              "nag": [
                "$1"
              ],
              "comment": "Best move in this position"
            }
          ]
        ]
      },
      {
        "san": "e5",
        "from": "e7",
        "to": "e5",
        "fen": "rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2"
      },
      {
        "san": "Nf3",
        "from": "g1",
        "to": "f3",
        "fen": "rnbqkbnr/pppp1ppp/8/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2"
      },
      {
        "san": "Nc6",
        "from": "b8",
        "to": "c6",
        "fen": "r1bqkbnr/pppp1ppp/2n5/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 2 3"
      },
      {
        "san": "Bb5",
        "from": "f1",
        "to": "b5",
        "fen": "r1bqkbnr/pppp1ppp/2n5/1B2p3/4P3/5N2/PPPP1PPP/RNBQK2R b KQkq - 3 3"
      },
      {
        "san": "a6",
        "from": "a7",
        "to": "a6",
        "fen": "r1bqkbnr/1ppp1ppp/p1n5/1B2p3/4P3/5N2/PPPP1PPP/RNBQK2R w KQkq - 0 4",
        "comment": "This opening is called the Ruy Lopez."
      },
      
      . . .
      
    (540 lines)

Usage for pgn_merge:

usage: pgn_merge.py [-h] [-o pathname] pathname splitter

positional arguments:
  pathname     path to your pgn file
  splitter     rules to split file:
                'w' - group games by white player and merge,
                'b' - group games by black player and merge,
                'a' - merge all games in file,
                custom: string with numbers separated with ':' where number is how much games 
                    in a row will be merged into one chapter, each number produce new chapter 
                    in result file.e.g.: '5:2:4' is 3 chapters with 5, 2 and 4 games merged,
                    rest of the games will be ignored.

optional arguments:
  -h, --help   show this help message and exit
  -o pathname  destination file name. default: [your_file_name]_edited.pgn

Example:

some_pgn_file.pgn
[Event "London knockout"]
[Site "London"]
[Date "1851.??.??"]
[Round "3.5"]
[White "Anderssen, Adolf"]
[Black "Staunton, Howard"]
[Result "1-0"]

1. e4 e6 2. d4 g6 3. Bd3 Bg7 4. Be3 c5 5. c3 cxd4 6. cxd4 Qb6 $6 {Winning a
pawn, but loosing time.} 7. Ne2 Qxb2 $6 8. Nbc3 Qb6 {White has developed all
his minor pieces.} 9. Rc1 Na6 10. Nb5 Bf8 $2 {Protecting the square d6, but
Black's pieces are completly misplaced.} (10... d6 $142) 11. O-O d6 1-0

[Event "World Cup"]
[Site "Reykjavik"]
[Date "1991.??.??"]
[Round "1"]
[White "Karpov, Anatoly"]
[Black "Speelman, Jonathan S"]
[Result "1-0"]
[WhiteElo "2730"]
[BlackElo "2630"]

1. e4 e6 2. d4 d5 3. Nd2 dxe4 4. Nxe4 Nd7 5. Nf3 Ngf6 6. Nxf6+ Nxf6 7. Bd3 c5
8. dxc5 Bxc5 9. Qe2 O-O $6 (9... Qc7 {is known to be safer.}) 10. Bg5 Qa5+ 1-0
now we can merge all games of this files with:
python3 pgn_merge.py some_pgn_file.pgn a
RESULT:
some_pgn_file_edited.pgn
[Event "?"]
[Site "?"]
[Date "????.??.??"]
[Round "?"]
[White "Chapter 1"]
[Black "?"]
[Result "*"]

1. e4 e6 2. d4 g6 ( d5 3. Nd2 dxe4 4. Nxe4 Nd7 5. Nf3 Ngf6 6. Nxf6+ Nxf6 7. Bd3 c5 8. dxc5 Bxc5 9. Qe2 O-O ( Qc7 ) 10. Bg5 Qa5+ ) 3. Bd3 Bg7 4. Be3 c5 5. c3 cxd4 6. cxd4 Qb6 7. Ne2 Qxb2 8. Nbc3 Qb6 9. Rc1 Na6 10. Nb5 Bf8 ( d6 ) 11. O-O d6   *  

Note:

Keep in mind: Merges affects only on games in single file. It is NOT that program which merge multiple pgn files in one. For such feature you can write your own program, just type in command line:

for windows:
type *.pgn > result.pgn

for linux:
cat *.pgn > result.pgn

congratulations! now you are programmer...

About

Convert your pgn db to JSON (works with variations, each move has FEN code of position). Group (merge) games in PGN db by player name (white or black) or your own rules.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages