3

I'm using pgn-extract to convert a PGN database into FEN + evaluations. My goal is something like this:

1. e4 e6 2. d4 b6 3. a3 Bb7

Into this:

rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1 { 1.20 } 
rnbqkbnr/pppp1ppp/4p3/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2 { 0.00 }
rnbqkbnr/pppp1ppp/4p3/8/3PP3/8/PPP2PPP/RNBQKBNR b KQkq d3 0 2 { 0.90 } 
rnbqkbnr/p1pp1ppp/1p2p3/8/3PP3/8/PPP2PPP/RNBQKBNR w KQkq - 0 3 { 0.70 }

So far, I've only been able to use: pgn-extract --quiet --evaluation -C --fencomments 1.pgn to get something like this (with both fen and evaluation following the SAN):

1. e4 { 1.20 } { rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0
1 } 1... e6 { 0.00 } { rnbqkbnr/pppp1ppp/4p3/8/4P3/8/PPPP1PPP/RNBQKBNR w
KQkq - 0 2 } 2. d4 { 0.90 } {
rnbqkbnr/pppp1ppp/4p3/8/3PP3/8/PPP2PPP/RNBQKBNR b KQkq d3 0 2 } 2... b6 {
0.70 } { rnbqkbnr/p1pp1ppp/1p2p3/8/3PP3/8/PPP2PPP/RNBQKBNR w KQkq - 0 3 }
3. a3 { 0.60 } { rnbqkbnr/p1pp1ppp/1p2p3/8/3PP3/P7/1PP2PPP/RNBQKBNR b KQkq
- 0 3 }

Is there a way to go directly to what I want?

3
  • 1
    Your example positions look rather like EPD, but you don't mention this format. EPD is basically a FEN position followed by various attributes, such as centipawn evaluation, following move, and so on. pgn-extract supports EPD: have you checked if it does something reasonably close to what you want?
    – user30536
    Jan 9 at 6:43
  • The most recent version of pgn-extract (21-08) has -Wfen as well as -Wepd for those two output formats. The problem is that both strip comments from the output so the --evaluation option would have no effect.
    – kentdjb
    Jan 9 at 7:16
  • On reflection, as EPD does support trailing comments via c0, c1, etc. it would be possible to extend the functionality of pgn-extract to retain move-related comments in the output. I am the author of pgn-extract so feel free to contact me via email if you would like to discuss this as a new feature.
    – kentdjb
    Jan 9 at 7:50

1 Answer 1

3

One way is by using the python chess library.

  • Install python
  • Install python chess with pip install chess

p.py

import chess.pgn

fn = 's.pgn'

with open(fn, encoding='utf-8') as h:
    while True:
        game = chess.pgn.read_game(h)
        if game is None:
            break

        for node in game.mainline():
            comment = node.comment
            com = comment.split('\n')
            fen = com[1]
            val = com[0]
            print(f'{fen} {{ {val} }}')

s.pgn

multiple game is supported.

[Event "Magnus Carlsen Inv KO"]
[Site "chess24.com INT"]
[Date "2021.03.17"]
[Round "1.22"]
[White "Carlsen,M"]
[Black "Aronian,L"]
[Result "1/2-1/2"]
[WhiteElo "2847"]
[BlackElo "2781"]
[ECO "C47"]

1. e4 { 1.20 } { rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1 } 1... e5 { -0.20 } { rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq e6 0 2 } 2. Nf3 { -0.20 } { rnbqkbnr/pppp1ppp/8/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2 } 2... Nc6 { -0.40 } { r1bqkbnr/pppp1ppp/2n5/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 2 3 } 3. Nc3 { -0.20 } { r1bqkbnr/pppp1ppp/2n5/4p3/4P3/2N2N2/PPPP1PPP/R1BQKB1R b KQkq - 3 3 } 3... Nf6 { 0.00 } { r1bqkb1r/pppp1ppp/2n2n2/4p3/4P3/2N2N2/PPPP1PPP/R1BQKB1R w KQkq - 4 4 } 4. d4 { 0.80 } { r1bqkb1r/pppp1ppp/2n2n2/4p3/3PP3/2N2N2/PPP2PPP/R1BQKB1R b KQkq d3 0 4 } 4... exd4 { -0.30 } { r1bqkb1r/pppp1ppp/2n2n2/8/3pP3/2N2N2/PPP2PPP/R1BQKB1R w KQkq - 0 5 } 5. Nxd4 { 1.30 } { r1bqkb1r/pppp1ppp/2n2n2/8/3NP3/2N5/PPP2PPP/R1BQKB1R b KQkq - 0 5 } 5... Bb4 { 0.30 } { r1bqk2r/pppp1ppp/2n2n2/8/1b1NP3/2N5/PPP2PPP/R1BQKB1R w KQkq - 1 6 } 6. Nxc6 { 4.20 } { r1bqk2r/pppp1ppp/2N2n2/8/1b2P3/2N5/PPP2PPP/R1BQKB1R b KQkq - 0 6 } 6... bxc6 { 0.40 } { r1bqk2r/p1pp1ppp/2p2n2/8/1b2P3/2N5/PPP2PPP/R1BQKB1R w KQkq - 0 7 } 7. Bd3 { 0.20 } { r1bqk2r/p1pp1ppp/2p2n2/8/1b2P3/2NB4/PPP2PPP/R1BQK2R b KQkq - 1 7 } 7... d5 { -0.60 } { r1bqk2r/p1p2ppp/2p2n2/3p4/1b2P3/2NB4/PPP2PPP/R1BQK2R w KQkq d6 0 8 } 8. exd5 { 0.80 } { r1bqk2r/p1p2ppp/2p2n2/3P4/1b6/2NB4/PPP2PPP/R1BQK2R b KQkq - 0 8 } 8... cxd5 { -0.30 } { r1bqk2r/p1p2ppp/5n2/3p4/1b6/2NB4/PPP2PPP/R1BQK2R w KQkq - 0 9 } 9. O-O { -0.10 } { r1bqk2r/p1p2ppp/5n2/3p4/1b6/2NB4/PPP2PPP/R1BQ1RK1 b kq - 1 9 } 9... O-O { 0.30 } { r1bq1rk1/p1p2ppp/5n2/3p4/1b6/2NB4/PPP2PPP/R1BQ1RK1 w - - 2 10 } 10. Bg5 { 0.90 } { r1bq1rk1/p1p2ppp/5n2/3p2B1/1b6/2NB4/PPP2PPP/R2Q1RK1 b - - 3 10 } 10... Be6 { 0.80 } { r2q1rk1/p1p2ppp/4bn2/3p2B1/1b6/2NB4/PPP2PPP/R2Q1RK1 w - - 4 11 } 11. Nb5 { 0.60 } { r2q1rk1/p1p2ppp/4bn2/1N1p2B1/1b6/3B4/PPP2PPP/R2Q1RK1 b - - 5 11 } 11... c5 { 0.70 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k1/p4p2/5q1p/3p1Bp1/3P4/P1r3P1/3Q1PP1/R3R1K1 b - - 0 25 } 25... Qxf5 { -1.20 } { 2r3k1/p4p2/7p/3p1qp1/3P4/P1r3P1/3Q1PP1/R3R1K1 w - - 0 26 } 26. Re5 { -1.10 } { 2r3k1/p4p2/7p/3pRqp1/3P4/P1r3P1/3Q1PP1/R5K1 b - - 1 26 } 26... Qd3 { -0.90 } { 2r3k1/p4p2/7p/3pR1p1/3P4/P1rq2P1/3Q1PP1/R5K1 w - - 2 27 } 27. Qxd3 { 10.20 } { 2r3k1/p4p2/7p/3pR1p1/3P4/P1rQ2P1/5PP1/R5K1 b - - 0 27 } 27... Rxd3 { -0.80 } { 2r3k1/p4p2/7p/3pR1p1/3P4/P2r2P1/5PP1/R5K1 w - - 0 28 } 28. Rxd5 { 0.10 } { 2r3k1/p4p2/7p/3R2p1/3P4/P2r2P1/5PP1/R5K1 b - - 0 28 } 28... Rc2 { 0.10 } { 6k1/p4p2/7p/3R2p1/3P4/P2r2P1/2r2PP1/R5K1 w - - 1 29 } 29. Ra5 { 0.30 } { 6k1/p4p2/7p/R5p1/3P4/P2r2P1/2r2PP1/R5K1 b - - 2 29 } 29... Rxd4 { -1.30 } { 6k1/p4p2/7p/R5p1/3r4/P5P1/2r2PP1/R5K1 w - - 0 30 } 30. Rxa7 { -0.20 } { 6k1/R4p2/7p/6p1/3r4/P5P1/2r2PP1/R5K1 b - - 0 30 } 30... Rdd2 { 0.60 } { 6k1/R4p2/7p/6p1/8/P5P1/2rr1PP1/R5K1 w - - 1 31 } 31. Rf1 { 0.40 } { 6k1/R4p2/7p/6p1/8/P5P1/2rr1PP1/5RK1 b - - 2 31 } 31... Ra2 { 0.70 } { 6k1/R4p2/7p/6p1/8/P5P1/r2r1PP1/5RK1 w - - 3 32 } 32. a4 { 0.50 } { 6k1/R4p2/7p/6p1/P7/6P1/r2r1PP1/5RK1 b - - 0 32 } 32... Kg7 { 0.50 } { 8/R4pk1/7p/6p1/P7/6P1/r2r1PP1/5RK1 w - - 1 33 } 33. Ra5 { 0.30 } { 8/5pk1/7p/R5p1/P7/6P1/r2r1PP1/5RK1 b - - 2 33 } 33... Rd4 { -0.30 } { 8/5pk1/7p/R5p1/P2r4/6P1/r4PP1/5RK1 w - - 3 34 } 34. Re1 { 0.50 } { 8/5pk1/7p/R5p1/P2r4/6P1/r4PP1/4R1K1 b - - 4 34 } 34... Rdxa4 { 0.20 } { 8/5pk1/7p/R5p1/r7/6P1/r4PP1/4R1K1 w - - 0 35 } 35. Rxa4 { 6.30 } { 8/5pk1/7p/6p1/R7/6P1/r4PP1/4R1K1 b - - 0 35 } 35... Rxa4 { -0.60 } { 8/5pk1/7p/6p1/r7/6P1/5PP1/4R1K1 w - - 0 36 } 36. f3 { -0.60 } { 8/5pk1/7p/6p1/r7/5PP1/6P1/4R1K1 b - - 0 36 } 36... g4 { -0.40 } { 8/5pk1/7p/8/r5p1/5PP1/6P1/4R1K1 w - - 0 37 } 37. fxg4 { 0.50 } { 8/5pk1/7p/8/r5P1/6P1/6P1/4R1K1 b - - 0 37 } 37... Rxg4 { -0.30 } { 8/5pk1/7p/8/6r1/6P1/6P1/4R1K1 w - - 0 38 } 38. Kf2 { 0.00 } { 8/5pk1/7p/8/6r1/6P1/5KP1/4R3 b - - 1 38 } 38... Rg5 { 0.10 } { 8/5pk1/7p/6r1/8/6P1/5KP1/4R3 w - - 2 39 } 39. Re4 { 0.20 } { 8/5pk1/7p/6r1/4R3/6P1/5KP1/8 b - - 3 39 } 39... Kg6 { 0.40 } { 8/5p2/6kp/6r1/4R3/6P1/5KP1/8 w - - 4 40 } 40. g4 { 0.60 } { 8/5p2/6kp/6r1/4R1P1/8/5KP1/8 b - - 0 40 } 40... Ra5 { 0.00 } { 8/5p2/6kp/r7/4R1P1/8/5KP1/8 w - - 1 41 } 41. Kg3 { -0.20 } { 8/5p2/6kp/r7/4R1P1/6K1/6P1/8 b - - 2 41 } 1/2-1/2

The move text must be in a single line, use -w 10000 option in pgn-extract.

pgn-extract --quiet --evaluation -C --fencomments -w 10000 1.pgn

Output

out.txt

rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1 { 1.20 }
rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq e6 0 2 { -0.20 }
rnbqkbnr/pppp1ppp/8/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2 { -0.20 }
r1bqkbnr/pppp1ppp/2n5/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 2 3 { -0.40 }
r1bqkbnr/pppp1ppp/2n5/4p3/4P3/2N2N2/PPPP1PPP/R1BQKB1R b KQkq - 3 3 { -0.20 }
r1bqkb1r/pppp1ppp/2n2n2/4p3/4P3/2N2N2/PPPP1PPP/R1BQKB1R w KQkq - 4 4 { 0.00 }
r1bqkb1r/pppp1ppp/2n2n2/4p3/3PP3/2N2N2/PPP2PPP/R1BQKB1R b KQkq d3 0 4 { 0.80 }
r1bqkb1r/pppp1ppp/2n2n2/8/3pP3/2N2N2/PPP2PPP/R1BQKB1R w KQkq - 0 5 { -0.30 }
r1bqkb1r/pppp1ppp/2n2n2/8/3NP3/2N5/PPP2PPP/R1BQKB1R b KQkq - 0 5 { 1.30 }
...

Command line

python p.py >out.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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