Python language function to convert a chess file in PGN notation to a file of FEN strings
Here's a basic PGN file (saved as euwe_colle_karlsbad_1929.pgn):
[Event "Karlsbad"] [Site "Karlsbad CSR"] [Date "1929.08.18"] [Round "15"] [White "Max Euwe"] [Black "Edgar Colle"] [Result "1-0"] 1.Nf3 Nf6 2.d4 e6 3.c4 b6 4.g3 Bb7 5.Bg2 Bb4+ 6.Bd2 Bxd2+ 7.Nbxd2 d6 8.O-O O-O 9.Re1 Nbd7 10.Qc2 e5 11.Nxe5 Bxg2 12.Nxd7 Bh3 13.Nxf8 1-0
Example calling code:
source_pgn = ".\\Data\\euwe_colle_karlsbad_1929.pgn" dest_fen = ".\\Data\\euwe_colle_karlsbad_1929.fen" print("Converting file " + source_pgn + " to FEN strings ") ChessFunctions.file_pgn_to_file_fen(source_pgn, dest_fen) print("Done ")
The resulting euwe_colle_karlsbad_1929.fen file contains:
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 rnbqkbnr/pppppppp/8/8/8/5N2/PPPPPPPP/RNBQKB1R b KQkq - 1 1 rnbqkb1r/pppppppp/5n2/8/8/5N2/PPPPPPPP/RNBQKB1R w KQkq - 2 2 rnbqkb1r/pppppppp/5n2/8/3P4/5N2/PPP1PPPP/RNBQKB1R b KQkq d3 0 2 rnbqkb1r/pppp1ppp/4pn2/8/3P4/5N2/PPP1PPPP/RNBQKB1R w KQkq - 0 3 rnbqkb1r/pppp1ppp/4pn2/8/2PP4/5N2/PP2PPPP/RNBQKB1R b KQkq c3 0 3 rnbqkb1r/p1pp1ppp/1p2pn2/8/2PP4/5N2/PP2PPPP/RNBQKB1R w KQkq - 0 4 rnbqkb1r/p1pp1ppp/1p2pn2/8/2PP4/5NP1/PP2PP1P/RNBQKB1R b KQkq - 0 4 rn1qkb1r/pbpp1ppp/1p2pn2/8/2PP4/5NP1/PP2PP1P/RNBQKB1R w KQkq - 1 5 rn1qkb1r/pbpp1ppp/1p2pn2/8/2PP4/5NP1/PP2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PGN to FEN conversion tool, under development.
The Wikipedia entries on PGN and FEN are quite good:
https://en.wikipedia.org/wiki/Portable_Game_Notation https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation
The FEN format is so simple there is no specification document. The PGN specification is at:
http://www.saremba.de/chessgml/standards/pgn/pgn-complete.htm